Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

c - What do the parentheses around a function name mean?

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

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...