我发现自己不小心在不同的地方使用了两种不同风格的 C 函数指针定义,因此决定编写一个最小的程序来测试这些差异。
有问题的两种样式是:
int (comp (int))
和
int (*comp)(int)
我写了一个最小的程序,对同一个函数指针使用这两种样式,一个用于函数的声明,一个用于同一函数的定义 , 看看编译器会怎么说。
声明:
int a_or_b (int (*a_fn)(), int (b_fn()), int (*comp)(int));
定义:
int a_or_b (int (a_fn()), int (*b_fn)(), int (comp(int)))
{
int out = a_fn ();
if (comp (out))
out = b_fn ();
return out;
}
正如您(希望)看到的那样,对于声明中的第一个参数,我使用了样式 int (*a_fn)()
,而在函数的定义中,我使用了样式 int (a_fn())
。我对接下来的两个参数做了类似的事情。
我推测这些可能是不兼容的类型,这些样式可能是变量的按引用传递和按值传递赋值之间的区别,但是当我编译这个程序时,编译器默默地愉快地编译了它.没有错误,没有警告,什么都没有。
因为我看过很多提倡第二种风格的教程,而出于审美目的我个人更喜欢第一种风格,我很想知道这两种风格之间的区别以及推荐哪种风格。
完整代码示例:
#include <stdio.h>
int a ();
int b ();
int a_or_b (int (*a_fn)(), int (b_fn()), int (*comp)(int));
int a ()
{
return 1;
}
int b ()
{
return 2;
}
int comparison (int param)
{
return param;
}
int a_or_b (int (a_fn()), int (*b_fn)(), int (comp(int)))
{
int out = a_fn ();
if (comp (out))
out = b_fn ();
return out;
}
int main (int argc, char **argv)
{
printf ("%i\n", a_or_b (a, b, comparison));
return 0;
}
最佳答案
在函数参数列表中,函数被转换为函数指针。 C11 标准,ISO/IEC 9899:2011,§6.7.6.3 函数声明符(包括原型(prototype)) 说:
¶8 A declaration of a parameter as "function returning type" shall be adjusted to "pointer to function returning type"…
因此,正如所写,参数都被视为“指向函数的指针”。不过,最好保持一致。
注意函数不能返回函数:
¶1 A function declarator shall not specify a return type that is a function type or an array type.
在函数声明符(函数定义或函数声明的参数列表)之外,符号是不同的:
int (*a_fn)();
int (b_fn());
其中第一个声明(定义)一个名为 a_fn
的变量 — 一个指向返回 int
的函数的指针,该函数具有未指定(但不是可变参数)参数列表。
其中第二个声明了一个名为 b_fn
的函数的存在,该函数返回一个 int
具有未指定(但不是可变参数)的参数列表(并且有一组多余的括号;可以写成 int b_fn();
意思是一样的)。
关于c - 这两种风格的 C 函数指针定义是否不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35923186/