Home > Backend Development > C++ > Why Can Function Names Be Used as Function Pointers in C90?

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

Susan Sarandon
Release: 2024-11-12 03:22:02
Original
1004 people have browsed it

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)();
Copy after login

Function Calls

All of the following express valid function calls:

(&f)();
f();
(*f)();
(**f)();
(***f)();

pf();
(*pf)();
(**pf)();
(***pf)();
Copy after login

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)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template