c - static structure initialization with function pointers -
i have initialized structure of static function name shown below.
we have initialize static structure constants. function names constants in c?
struct fp { int (*fn)(); }; int f1() { printf("f1 called \n"); return 0; } static struct fp fps = { .fn = f1, }; int main() { fps.fn(); return 0; }
if compiling without issues when initialized structure shown below.
static struct fp fps = { .fn = &f1, };
in c function name both f1 , &f1 same?
f1
name of function. in contexts, when used expression, function name decays automatically pointer function. 1 context decay doesn't happen when function name operand of address-of operator (&
). evaluated expressions, f1
, &f1
same.
in fact, in any function call expression f(args)
, f
pointer function. reason can call functions name because of automatic decay function name function pointer.
you can turns around , dereference function pointers, too. they'll decay pointer right away:
int f(void); f(); // "f" decays &f (&f)(); // normal call via function pointer, no implicit decay (*f)(); // why not (*****f)(); // ditto
Comments
Post a Comment