#include<stdio.h>
#include<stdlib.h>
int fun1()
{
printf("I am fun1.");
return 0;
}
int fun2(int fun())
{
fun();
return 0;
}
int main()
{
fun2(fun1);
return 0;
}
The above program can run. As far as I am concerned, I can understand int fun2(int (*fun)()), but I do not know how int fun2(int fun()) works. Thank you.
When you write
int fun2(int fun()), the parameterint fun()converts intoint (*fun)(), it becomes exactly equivalent to this:A more famiiar conversion happens in case of array when you declare it as function parameter. For example, if you’ve this:
Even here the parameter type converts into
int*, and it becomes this:The reason why function type and array type converts into function pointer type, and pointer type, respectively, is because the Standard doesn’t allow function and array to be passed to a function, neither can you return function and array from a function. In both cases, they decay into their pointer version.
The C++03 Standard says in §13.1/3 (and it is same in C++11 also),
And a more interesting discussion is here: