17
foo();

(*foo)();

(&foo)();

What exactly is the difference between these function calls (assuming foo() is defined somewhere)? and are there any situations where one might be used over another?

Also, why don't &foo() and *foo() work?

1
  • 9
    How is foo defined?
    – CB Bailey
    Aug 20, 2012 at 13:37

6 Answers 6

25

There is no difference between the actual calls themselves (rather, they will all do the same thing depending on how foo() is declared)

All function calls in C and C++ take place via a function-pointer expression which appears before the function call parentheses. Implicit address-of of non-pointer types takes place if necessary.

Here's an ideone demonstrating the behavior in C++.

The reason &foo() and *foo() don't work is that the function call operator () takes precedence over * and &. So they might work, depending on what you were doing with the return value. &foo() would take the return value's address, and *foo() would dereference it. Under some circumstances, either of these operations, or both, might be legal. Consider a function returning a reference-to-pointer type.

Part of this answer taken from R..'s comment.

6
  • 1
    No, you're absolutely right. All function calls in C take place via a function-pointer expression which appears before the function call parentheses. In the case of &foo, it's an explicit pointer. In the case of foo, it's the implicit conversion of a function to its address (much like arrays) when using its name. And in the case of *foo, first the function name implicitly converts to a pointer, then it gets dereferenced to yield a function type, then it implicitly converts to a pointer again. :-) Aug 20, 2012 at 13:42
  • 5
    I'm just trying to figure out who downvoted, and why. People who downvote without explanation are silly. Maybe they just missed the upvote button. ;D
    – Wug
    Aug 20, 2012 at 13:44
  • @R.. I guess making that explanation an answer or adding it to this one would really help understand why this works. I guessed something along those lines, but it isn't really one of the langauge's most obvious behaviours. Aug 20, 2012 at 13:47
  • Can you ever take the address of a return value that hasn't been stored somewhere? In C++ you can return a valid lvalue from a function but I don't think C has any facility for doing that.
    – Matt K
    Aug 20, 2012 at 14:08
  • C doesn't have references, so doing so will give you a pointer to a temporary object. It's syntactically legal, but not useful because attempting to use it will result in undefined behavior.
    – Wug
    Aug 20, 2012 at 14:15
10

You don't say exactly what foo is, but I'll assume it's a function.

What exactly is there difference between these function calls?

Obviously, the first calls the function using the usual syntax.

The third takes the address of the function and attempts to call that; the language allows function pointers to be called as if they were the function they point to, so this is equivalent to foo().

The second tries to dereference the function. Dereferencing requires a pointer, and the language allows implicit conversion from a function to a pointer to that function, so this is equivalent to (*(&foo))(), which in turn is equivalent to foo().

To summarise: all three do the same thing.

and are there any situations where one might be used over another?

Unless you like to decorate your code with unnecessary heiroglyphics, there's no reason to use anything other than the first form, for either functions or function pointers.

Also, why don't &foo() and *foo() work?

The precedence rules mean that these are equivalent to &(foo()) and *(foo()); i.e. they call the function and try to take the address of and dereference the result. The first form will "work" if the function has a return type; the second will "work" if it returns a pointer or (in C++) something with an overloaded unary operator*().

0

foo() calls function foo.

void foo() {return;}
foo();

(*foo)() dereferences a (function) pointer named foo, and calls it with zero arguments.

void bar() {return;}

int main(int argc, char** argv){
    void(*foo)() = &bar;
    (*foo)(); // works
    (*bar)(); // also works
    return 0;
}

(&foo)() takes the reference to a function foo, and calls it with zero arguments.

void bar() {return;}

int main(int argc, char** argv){
    (&bar)();
    return 0;
}

Hope that helped.

2
  • you forgot to answer the second part of the question, which is why *foo() and &foo() don't work the same way sans parentheses.
    – Wug
    Aug 20, 2012 at 13:56
  • You're right. Its moot now though, seeing as your answer already answers his question better anyways :)
    – Moritz
    Aug 20, 2012 at 14:05
0

If foo is a function designator, these are all equivalent in C:

foo();
(foo)();
(*foo)();
(***foo)();
(*&foo)();
0
foo();

Calls a function named foo.

(*foo)();

Calls a function through a pointer to a function named foo; you typically see this when invoking a function that has been passed as a parameter to another function (also known as a callback):

/**
 * Execute the function pointed to by "foo" for each element
 * in the array.
 */
void map(int *arr, size_t arrsize, void (*foo)(int))
{
  size_t i;
  for (i = 0; i < arrsize; i++)
    (*foo)(arr[i]); // can also be called as foo(arr[i]);
}

void bar(int i)
{
  printf("i = %d\n", i);
}

int main(void)
{
  int arr[] = {1, 2, 3, 4, 5};
  /**
   * Call the function "bar" for each member
   * of "arr".
   */
  map(arr, sizeof arr / sizeof *arr, bar);
}

The mapping in this example is kind of stupid, but it illustrates the concept. Just like you can have pointers to different data types, you can have pointers to different function types. Instead of executing a function named foo, we're executing a function that's being pointed to by foo.

Note that parentheses matter: the function call operator () has a higher precedence than unary *, so the expression *foo() would be interpreted as "call foo and dereference the result", whereas (*foo)() is interpreted as "dereference foo and call the result".

(&foo)();

does the same thing as (*foo)();, except that it's working through a reference instead of a pointer (C++ only). Again, parentheses matter; &foo() is interepreted as "call foo and take the address of the result", which isn't legal.

0

Since this is tagged c++, it's possible for a function to return a reference in that language, and such a function could be called as &foo():

#include <iostream>

using std::cout;
using std::endl;

int & foo() {
    static int f = 5;
    return f;
}

int main() {
    cout << foo() << endl;
    *(&foo()) = 7;
    cout << foo() << endl;
    return 0;
}      
5
  • I am sorry, but what does this have to do with the question? He was talking about function pointers. The signature for that would be int& (*foo)(void) but that wasn't what he asked.
    – Cubic
    Aug 20, 2012 at 16:27
  • Reread the question. "Assuming foo() is defined somewhere" "Also, why don't &foo() and *foo() work?" The answer to the latter part is that they can in some circumstances.
    – Matt K
    Aug 20, 2012 at 17:49
  • That has nothing to do with the function returning a reference. The reason it doesn't work is because () has a greater precedence than *, so you have to use parantheses. foo, &foo and *foo all end up breaking down to the same function pointer in that situation.
    – Cubic
    Aug 20, 2012 at 17:58
  • The reason it doesn't work is that a temporary is not an lvalue. It's syntactically valid. *foo() makes sens for functions that return a pointer. I guess by "work" I mean they compile and do something which includes calling the function.
    – Matt K
    Aug 20, 2012 at 18:17
  • Look at his first source example. He wrote (&foo)() and was then asking why he couldn't just write &foo().
    – Cubic
    Aug 20, 2012 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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