"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Can Function Names Be Used as Function Pointers in C90?

Why Can Function Names Be Used as Function Pointers in C90?

Published on 2024-12-23
Browse:132

Why Can Function Names Be Used as Function Pointers in C90?

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)
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3