摘自《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) ? }
问题最后一个函数的返回值不应该是*f(first_in, second_in)
吗,因为f
是一个指向函数的指针和 *f
表示实际函数?
最佳答案
调用函数指针的解引用运算符是optional in C .
关于c - 返回对 C 中函数指针的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40074937/