c++ - C function pointers -


static void increment(long long *n){   (*n)++; }  struct test{   void (*work_fn)(long long *); };  struct test t1;  t1.work_fn = increment; 

how call function now? t1.work_fn(&n) ?

how call function now? t1.work_fn(&n) ?

that'll work fine.

function pointers don't need explicitly dereferenced. because when calling function (using actual name of function), you're calling through pointer function. c99 6.5.22 "function calls" says (emphasis mine):

the expression denotes called function (footnote 77) shall have type pointer function returning void or returning object type other array type

footnote 77:

most often, result of converting identifier function designator.

note still can dereference function pointer (or normal function name - though think you'd cause confusion doing so) call function because c99 6.5.3.2/4 "address , indirection operators" says:

the unary * operator denotes indirection. if operand points function, result function designator

so of these end doing same thing (though compiler might not able optimize calls-through t1.work_fn well):

t1.work_fn(&n); (*t1.work_fn)(&n);  increment(&n); (*increment)(&n); 

Comments