c - 功能引用参数: pointer vs prototype merely style?

标签 c parameters reference prototype

您可以声明像这样的函数引用参数吗?

int g ( int (*f)(int,int) ) {

或者

int g ( int f(int,int) ) {

以我有限的 C 经验,我从未见过原型(prototype)方法。

最佳答案

我认为这是风格,但是指向函数符号的显式指针符合古老的实践以及编译器解释代码的方式。

C11* 标准在 §6.7.6.3 Function declarators (including prototypes) ¶8 中表示:

¶8 A declaration of a parameter as "function returning type" shall be adjusted to "pointer to function returning type", as in 6.3.2.1.

§6.3.2.1 Lvalues, arrays and function designators ¶4说:

¶4 A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, the _Alignof operator,65) or the unary & operator, a function designator with type "function returning type" is converted to an expression that has type "pointer to function returning type".

65) Because this conversion does not occur, the operand of the sizeof or _Alignof operator remains a function designator and violates the constraints in 6.5.3.4.

因此,使用指针表示法可以明确显示编译器将执行的操作。 (我仍然通常使用 (*function_ptr)(arg1, …, argN) 表示法调用指向函数的显式指针,尽管 function_ptr(arg1, …, argN) 也可以工作.)


* C18 标准是最新的,但目前还没有方便的在线 HTML 版本。大多数情况下,两者非常相似。碰巧,§6.3.2.1 ¶4 是一个有差异的段落 - C18 版本没有提及 _Alignof

关于c - 功能引用参数: pointer vs prototype merely style?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51851168/

相关文章:

HTML 表单 - 当 URL 包含 index.aspx 时,action=URL 的输入类型提交问题

c# - 通过 RealProxy 引用参数

c - 使用 'dummy' 指针仅用于比较

c - 用位域初始化结构常量数组

python - PyCharm 显示 PyQt 方法的错误参数信息

c++ - 主要功能参数的属性

c++ - 如何使用此 OpenMP 关键部分避免此原始指针?

c# - Visual Studio 项目 : How to include a reference for one configuration only?

c - 跳入 C 中的一个 block

c - 通过 func ptr 间接将参数推送到函数