如何声明一个返回函数指针的函数?
想象一个带有参数 double 和 int 的函数 myFunctionA:
myFunctionA (double, int);
该函数应该返回一个函数指针:
char (*myPointer)();
如何在 C 中声明该函数?
Imagine a function myFunctionA with the parameter double and int:
myFunctionA (double, int);
This function should return a function pointer:
char (*myPointer)();
How do I declare this function in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
typedef
是你的朋友:typedef
is your friend:根据 right-left-rule,
fun
是一个double, int
的函数,返回一个指向参数不确定的函数的指针,返回void
。编辑:这是该规则的另一个链接。
编辑2:这个版本只是为了紧凑并表明它确实可以完成。
在这里使用 typedef 确实很有用。但不是指向指针,而是指向函数类型本身。
为什么?因为可以将它用作一种原型,从而确保功能确实匹配。并且因为作为指针的身份仍然可见。
所以一个好的解决方案是
According to the right-left-rule,
fun
is a function ofdouble, int
returning a pointer to a function with uncertain parameters returningvoid
.EDIT: This is another link to that rule.
EDIT 2: This version is only for the sake of compactness and for showing that it really can be done.
It is indeed useful to use a typedef here. But not to the pointer, but to the function type itself.
Why? Because it is possible to use it as a kind of prototype then and so ensure that the functions do really match. And because the identity as a pointer remains visible.
So a good solution would be
制作一个类型定义:
Make a typedef: