In one of my project source files, I found this C function definition:
在我的一个项目源文件中,我发现了这个C函数定义:
int (foo) (int *bar)
{
return foo (bar);
}
Note: there is no asterisk next to foo
, so it's not a function pointer. Or is it? What is going on here with the recursive call?
注意:foo旁边没有星号,所以它不是函数指针。或者是吗?递归调用是怎么回事?
3 个解决方案
#1
326
In the absence of any preprocessor stuff going on, foo
's signature is equivalent to
在没有任何预处理器的情况下,foo的签名等同于
int foo (int *bar)
The only context in which I've seen people putting seemingly unnecessary parentheses around function names is when there are both a function and a function-like macro with the same name, and the programmer wants to prevent macro expansion.
我所见过的人们在函数名周围加上看似不必要的圆括号的唯一上下文是,当函数和类函数的宏具有相同的名称时,程序员希望防止宏扩展。
This practice may seem a little odd at first, but the C library sets a precedent by providing some macros and functions with identical names.
这种做法乍一看可能有点奇怪,但是C库通过提供一些具有相同名称的宏和函数来设置一个先例。
One such function/macro pair is isdigit()
. The library might define it as follows:
一个这样的函数/宏对是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 */
}
Your function looks almost identical to the above, so I suspect this is what's going on in your code too.
你的函数看起来几乎和上面的一样,所以我怀疑这就是你的代码。
#2
35
The parantheses don't change the declaration - it's still just defining an ordinary function called foo
.
这些偏执狂并没有改变声明——它只是定义了一个普通的函数foo。
The reason that they have been used is almost certainly because there is a function-like macro called foo
defined:
之所以使用它们,几乎可以肯定是因为有一个类函数的宏叫做foo定义:
#define foo(x) ...
Using (foo)
in the function declaration prevents this macro from being expanded here. So what is likely happening is that a function foo()
is being defined with its body being expanded from the function-like macro foo
.
在函数声明中使用(foo)可以防止这个宏在这里展开。因此,可能发生的情况是,函数foo()正在被定义,它的主体从功能类似的宏foo扩展。
#3
-2
The parentheses are meaningless.
The code you show is nothing but an infinite recursion.
括号是毫无意义的。您所显示的代码只是一个无限递归。
When defining a function pointer, you sometimes see strange parentheses that do mean something. But this isn't the case here.
在定义函数指针时,有时会看到奇怪的括号,表示某些内容。但这里不是这样。
#1
326
In the absence of any preprocessor stuff going on, foo
's signature is equivalent to
在没有任何预处理器的情况下,foo的签名等同于
int foo (int *bar)
The only context in which I've seen people putting seemingly unnecessary parentheses around function names is when there are both a function and a function-like macro with the same name, and the programmer wants to prevent macro expansion.
我所见过的人们在函数名周围加上看似不必要的圆括号的唯一上下文是,当函数和类函数的宏具有相同的名称时,程序员希望防止宏扩展。
This practice may seem a little odd at first, but the C library sets a precedent by providing some macros and functions with identical names.
这种做法乍一看可能有点奇怪,但是C库通过提供一些具有相同名称的宏和函数来设置一个先例。
One such function/macro pair is isdigit()
. The library might define it as follows:
一个这样的函数/宏对是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 */
}
Your function looks almost identical to the above, so I suspect this is what's going on in your code too.
你的函数看起来几乎和上面的一样,所以我怀疑这就是你的代码。
#2
35
The parantheses don't change the declaration - it's still just defining an ordinary function called foo
.
这些偏执狂并没有改变声明——它只是定义了一个普通的函数foo。
The reason that they have been used is almost certainly because there is a function-like macro called foo
defined:
之所以使用它们,几乎可以肯定是因为有一个类函数的宏叫做foo定义:
#define foo(x) ...
Using (foo)
in the function declaration prevents this macro from being expanded here. So what is likely happening is that a function foo()
is being defined with its body being expanded from the function-like macro foo
.
在函数声明中使用(foo)可以防止这个宏在这里展开。因此,可能发生的情况是,函数foo()正在被定义,它的主体从功能类似的宏foo扩展。
#3
-2
The parentheses are meaningless.
The code you show is nothing but an infinite recursion.
括号是毫无意义的。您所显示的代码只是一个无限递归。
When defining a function pointer, you sometimes see strange parentheses that do mean something. But this isn't the case here.
在定义函数指针时,有时会看到奇怪的括号,表示某些内容。但这里不是这样。