Why am I able to assign a function that returns an int with no parameters specified to a function pointer that returns an int but takes an int argument?
For example when I do:
int f()
{ return 0;}
int (*ptr)(int) = f;
the compiler gives me no warnings
In C,
fdoesn’t take “no arguments”, but rather “any arguments”*. Sayint f(void)to declare “no arguments”.This is different from C++. Notably, C has separate notions of function “declaration” and function “prototype”:
*) As I said in the comment, “any arguments” is restricted to types that do not suffer default-promotion (in the same way as default-promotion happens to variadic arguments). That is,
float, all flavours ofcharand ofshort int, and also..., are not permissible in the actual function signature.