How to distinguish if a function pointer in C points to function 1 or function 2 -
How to distinguish if a function pointer in C points to function 1 or function 2 -
this question has reply here:
is safe pass function pointers around , compare them normal object's pointers? 6 answersi have 2 functions
void foo1(int); void foo2(int);
i.e. both have same signature. have function pointer
typedef void (*func_pt)(int)
that can point either foo1 or foo2 e.g.
if (match_condition) { func_pt = foo1; } else { func_pt = foo2; }
later in code, need find out if func_pt pointing foo1 or foo2.
this code if (func_pt == foo1)
seems work.
is recommended , safe? if not, there alternative findng out function function pointer point to?
i don't understand how c compiler can figure out equality. when compiler sees foo1, finds address of code function instructions stored?
thanks answers
yes, safe. [6.5.9] of c99 standard (emphasis mine):
two pointers compare equal if , if both null pointers, both pointers same object (including pointer object , subobject @ beginning) or function, both pointers 1 past lastly element of same array object, or 1 pointer 1 past end of 1 array object , other pointer start of different array object happens follow first array object in address space.
c function-pointers
Comments
Post a Comment