繁体   English   中英

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

[英]Returning a call to a pointer to a function in C

来自21 世纪 C一书:

从概念上讲,函数类型的语法实际上是指向给定类型函数的指针。 如果您有一个带有标题的函数,例如:

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

然后只需添加一个星号(和括号来解决优先级)来描述指向此类函数的指针:

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

然后将 typedef 放在它前面来定义一个类型:

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

现在您可以将它用作任何其他类型,例如声明一个将另一个函数作为输入的函数:

 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表示实际的函数?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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