Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does having a int* (float) point to a int() have a warning, but int* (double) don't?

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading