您可以声明像这样的函数引用参数吗?
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/