I have test the following code:
#include <stdio.h>
void f(void g()) {
g();
}
void g(void) {
printf("hello, world\n");
}
int main() {
f(g);
return 0;
}
It works well. In the code above, why in c we can take the function name g
as paramter and take the function prototype as parameter?
In book K&R, it says that we can use the pointer to function:
So I can define the function f
like this:
void f(void (*g)()) {
g();
}
void g(void) {
printf("hello, world\n");
}
int main() {
void (*pf)() = g;
f(pf);
f(g); // Here f(g) also works.
return 0;
}
So in the first f
definition form, the parameter prototype is void g()
, in the second form the parameter prototype is void (*g)()
. Why the two forms of definition of f
are the same?
And in the second form, the parameter is a pointer to function, why f(g)
also works?