This question already has an answer here:
- Writing a function pointer in c 3 answers
- How do you read C declarations? 10 answers
Can anyone please explain what int ((*foo(int)))(int)
in this does?
int (*fooptr)(int);
int ((*foo(int)))(int); // Can't understand what this does.
int main()
{
fooptr = foo(0);
fooptr(10);
}
.
There already answers to this, but I wanted to approach it in the opposite way.
A function declaration looks the same as a variable declaration, except that the variable name is replaced by the function name and parameters.
So this declares
bar
as a pointer to a function that takes anint
and returns anint
:If, instead of a variable
bar
, it's a functionfoo(int)
with that return value, you replacebar
withfoo(int)
and get:Add an unnecessary pair of parentheses and you get:
This declares
foo
as a function that expects anint
type argument and returns a pointer to a function that expects anint
type argument and return anint
.To be more clear:
Here is a good explanation for the same.
According to cdecl,
foo
is:is what we declare.
It is a function that takes a single
int
argumentand returns a pointer
to a function that takes a single
int
argumentand returns an
int
.One pair of
()
is redundant. The same thing can be expressed as