cwarningsfunction-pointers

Why warning in C and can't compile in C++?


Why does this code

int (*g)(int);
int (*h)(char);
h = g;

In C, give me such warning when compiling:

'warning: assignment from incompatible pointer type'

In C++, can't be able to compile.


Solution

  • Prototypes don't match. g is a pointer to a function which takes an int and returns an int whereas h is a pointer to a function which takes a char and returns an int. They are two separate types and hence the warning (assigning apples to oranges).