c - 返回对 C 中函数指针的调用

标签 c pointers

摘自《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/

相关文章:

c - 与不相等的字符串比较时,strcmp() 返回 0

c - 套接字没有等待预期的 10 秒

c - 使用指针了解数组的大小

c - void swap(int *a, int *b) 不适用于数组

c - C 中用户定义的数组大小

C - 索引应用于指针到指针

说明此 C 代码为何有效

c++ - 我是否正确初始化了我的 C++ 引用变量?

c - 指针输出结果

c++ - 这个‘type variableofType()’是函数还是对象?