I have this piece of code:
int foo(){ return 0;}
int main()
{
int (*float_function)(float) = foo;
}
Compile using x86-64 gcc 12.2
, with -Wall
, it produces the warning (Link):
warning: initialization of 'int (*)(float)' from incompatible pointer type 'int (*)()' [-Wincompatible-pointer-types]
but when I change from float
to double
(Link):
int foo(){ return 0;}
int main()
{
int (*double_function)(double) = foo;
}
the warning is now gone.
But I think that both of these should get a warning.
Am I wrong somewhere? Why does gcc not complain on the second example?
CodePudding user response:
int foo()
is declared without specifying its parameters. This is an obsolete feature that lets you call it with any arguments. When calling the function, integer arguments are promoted to int
(if needed), and float
arguments are promoted to double
.
Due to this, it's impossible for this function to receive a float
parameter, which makes it incompatible with int (*)(float)
, but not with int (*)(double)
.
If you want a function that takes no parameters, declare it as int foo(void)
which will make it incompatible with both.