I have the following code:
#include <stdio.h>
typedef void (*myfunc_t)(int x);
myfunc_t myfunc(int x)
{
printf("x = %d\n", x);
return;
}
int main(void)
{
myfunc_t pfunc = myfunc;
(pfunc)(1);
return 0;
}
When compiling for C99, standard C, I get an error:
prog.c: In function ‘myfunc’:
prog.c:9:6: error: ‘return’ with no value, in function returning non-void [-Werror]
return;
^~~~~~
prog.c:5:14: note: declared here
myfunc_t myfunc(int x)
^~~~~~
prog.c: In function ‘main’:
prog.c:14:26: error: initialization of ‘myfunc_t’ {aka ‘void (*)(int)’} from incompatible pointer type ‘void (* (*)(int))(int)’ [-Werror=incompatible-pointer-types]
myfunc_t pfunc = myfunc;
^~~~~~
A few questions in SO already explain that the return type of myfunc_t
is void
(e.g., here). So, why do I get these errors?
Note that if I change the type of myfunc
from myfunc_t
to void
, then the program builds OK.