use*_*094 213 c function parentheses
在我的一个项目源文件中,我发现了这个C函数定义:
int (foo) (int *bar)
{
return foo (bar);
}
Run Code Online (Sandbox Code Playgroud)
注意:旁边没有星号foo,因此它不是函数指针.或者是吗?递归调用会发生什么?
NPE*_*NPE 329
在没有任何预处理器的情况下,foo签名相当于
int foo (int *bar)
Run Code Online (Sandbox Code Playgroud)
我看到人们在函数名称周围放置看似不必要的括号的唯一上下文是当函数和类似函数的宏都具有相同的名称时,程序员想要阻止宏扩展.
这个实践起初看起来有点奇怪,但是C库通过提供一些具有相同名称的宏和函数来开创先例.
一个这样的函数/宏对是isdigit().库可能会将其定义如下:
/* the macro */
#define isdigit(c) ...
/* the function */
int (isdigit)(int c) /* avoid the macro through the use of parentheses */
{
return isdigit(c); /* use the macro */
}
Run Code Online (Sandbox Code Playgroud)
你的功能看起来几乎与上面的相同,所以我怀疑这也是你的代码中发生的事情.
caf*_*caf 37
parantheses不会改变声明 - 它仍然只是定义一个名为的普通函数foo.
它们被使用的原因几乎可以肯定是因为有一个类似函数的宏被foo定义:
#define foo(x) ...
Run Code Online (Sandbox Code Playgroud)
使用(foo)函数声明防止这里被扩展这个宏.所以可能发生的事情是foo()正在定义一个函数,它的主体从类似函数的宏扩展foo.