简体   繁体   中英

Returning a call to a pointer to a function in C

From the book 21st Century C :

Conceptually, the syntax for a function type is really a pointer to a function of a given type. If you have a function with a header like:

 double a_fn(int, in); //a declaration

then just add a star (and parens to resolve precedence) to describe a pointer to this type of function:

 double (*a_fn_type)(int, int); //a type: pointer-to-function

Then put typedef in front of that to define a type:

 typedef double (*a_fn_type)(int, int); //a typedef for a pointer to function

Now you can use it as a type like any other, such as to declare a function that takes another function as input:

 double apply_a_fn(a_fn_type f, int first_in, int second_in){ return f(first_in, second_in); //shouldn't this be *f(first_in, second_in) ? }

Question: Shouldn't the return value of this very last function be *f(first_in, second_in) , since f is a pointer to a function and *f denotes the actual function?

用于调用函数指针的解引用运算符在 C 中可选的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM