224

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?

6
  • 8
    No, it is not a function pointer - it is still a regular function named foo. Nov 28, 2012 at 8:29
  • 1
    Is this the complete function ?
    – asheeshr
    Nov 28, 2012 at 8:30
  • 3
    do you have evidence that this function is used in a useful context?
    – moooeeeep
    Nov 28, 2012 at 8:32
  • 1
    ...looks like some dummy function that was perhaps just written to see if it compiles, in existing source, and should have been removed. I'd remove it (if that is what the function really does), since at best it will be infinite loop (I'm not sure if C compiler is allowed to optimize that tail call to jump), at worst stack overflow.
    – hyde
    Nov 28, 2012 at 8:33
  • 3
    Parentheses in C declarations help to amke the language ambiguous. Quick, what is a(b);? Declaration of b as a variable of type a? Or a call to function a with argument b? The difference is syntactic, and you cannot know which way to even parse it without looking up the declaration info of a; i.e. are those postfix function call parentheses, or optional parentheses around a declarator.
    – Kaz
    Nov 28, 2012 at 20:02

3 Answers 3

344

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.

9
  • 2
    That may be the case here as well; I didn't look for macros... And I did not knew that macro expansion does not take place within parentheses, Thanks for pointing that out! Nov 28, 2012 at 8:49
  • 13
    @user1859094: On second look, this is almost certainly what's going on in your code. The foo(bar) inside the function is using the corresponding macro.
    – NPE
    Nov 28, 2012 at 9:09
  • 79
    @user1859094 macro expansion does take place within parentheses, but expansion of a function-like macro only takes place if the next token is a left parenthesis (C99, 6.10.3§10), thus foo (int* bar) would get replaced, but not (foo) (int *bar) (the next token after foo is ))
    – Virgile
    Nov 28, 2012 at 9:48
  • 4
    How would such a function be called? Would you call it with the parentheses as well? For example, would this work: (isdigit)(5)?
    – gcochard
    Dec 5, 2012 at 19:14
  • 4
    @Greg: Right, that's exactly how you'd call it.
    – NPE
    Dec 5, 2012 at 19:27
38

The parantheses don't change the declaration - it's still just defining an ordinary function called foo.

The reason that they have been used is almost certainly because there is a function-like macro called foo defined:

#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.

3
  • 5
    Nice deduction (though using parentheses for this purpose should be punishable by law).
    – ugoren
    Nov 28, 2012 at 8:47
  • 3
    @ugoren: using parens around the function name is about the only way to prevent a macro expansion for a function-like macro. At times it's a necessary tool. Nov 28, 2012 at 9:19
  • 7
    @MichaelBurr, there's also the option of not having a macro and function with the same name. I know you can't always control everything, but if you reached this solution, I'd say something is very wrong.
    – ugoren
    Nov 28, 2012 at 9:23
-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.

2
  • 9
    Evidently not; the parentheses prevent macro expansion. See the accepted answer.
    – Kevin
    Nov 28, 2012 at 22:53
  • 13
    @Kevin, My answer is about the code shown, and is correct for it. In almost any C question here, assuming unknown preprocessor definitions can change everything. In this case, answers that consider the preprocessor are indeed better, but it doesn't make mine incorrect.
    – ugoren
    Nov 29, 2012 at 9:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.