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?
Calling and Called Function ? The Function which calls another Function is called Calling Function and function which is called by another Function is call Called Function. How does Function execution work? A stack data structure is used during the execution of the function calls.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.
In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument. In the case of Call by Reference, when we pass the parameter's location reference/address, it copies and assigns them to the function's local argument.
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.
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*()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With