Interview Questions

I have seen different syntax used for calling functions via pointers ...

C Interview Questions and Answers


(Continued from previous question...)

I have seen different syntax used for calling functions via pointers ...

Originally, a pointer to a function had to be ``turned into'' a ``real'' function, with the * operator, before calling:
int r, (*fp)(), func();
fp = func;
r = (*fp)();
The interpretation of the last line is clear: fp is a pointer to function, so *fp is the function; append an argument list in parentheses (and extra parentheses around *fp to get the precedence right), and you've got a function call.

It can also be argued that functions are always called via pointers, and that ``real'' function names always decay implicitly into pointers (in expressions, as they do in initializations; This reasoning means that
r = fp();
is legal and works correctly, whether fp is the name of a function or a pointer to one. (The usage has always been unambiguous; there is nothing you ever could have done with a function pointer followed by an argument list except call the function pointed to.)
The ANSI C Standard essentially adopts the latter interpretation, meaning that the explicit * is not required, though it is still allowed.

(Continued on next question...)

Other Interview Questions