函数名周围的括号是什么意思? [英] What do the parentheses around a function name mean?

查看:26
本文介绍了函数名周围的括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个项目源文件中,我发现了这个 C 函数定义:

In one of my project source files, I found this C function definition:

int (foo) (int *bar)
{
    return foo (bar);
}

注意:foo 旁边没有星号,所以它不是函数指针.或者是吗?递归调用这里发生了什么?

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 的签名等价于

In the absence of any preprocessor stuff going on, foo's signature is equivalent to

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.

这种做法乍一看似乎有点奇怪,但 C 库开创了先例,提供一些同名的宏和函数.

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.

一个这样的函数/宏对是isdigit().库可能定义如下:

One such function/macro pair is isdigit(). The library might define it as follows:

/* 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.

这篇关于函数名周围的括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆