Using Function Names as Function Pointers
C90's rationale document provides insight into the design choice of equating function names with function pointers. This convenience simplifies using function pointers in specific contexts.
Function Declarations
Consider the declarations:
int f(); int (*pf)();
Function Calls
All of the following express valid function calls:
(&f)(); f(); (*f)(); (**f)(); (***f)(); pf(); (*pf)(); (**pf)(); (***pf)();
The first expression on each line was covered previously. The second is conventional. Subsequent expressions imply an implicit conversion of the function designator to a pointer value in most contexts.
Design Rationale
The committee saw no significant drawbacks to allowing these forms and viewed outlawing them as excessive effort. Thus, the equivalence between function designators and function pointers offers convenience in pointer usage.
Implicit Conversion
Another interesting observation is the implicit conversion of function types to pointers when used as parameters but not as return types:
typedef bool FunctionType(int); void g(FunctionType); // Implicitly converts to void g(FunctionType *) FunctionType h(); // Error FunctionType *j(); // Returns a function pointer with the type bool(int)
The above is the detailed content of Why Can Function Names Be Used as Function Pointers in C90?. For more information, please follow other related articles on the PHP Chinese website!