I have a small program that uses function pointers.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _myst
{
int a;
char b[10];
}myst;
void cbfunc(myst *mt)
{
fprintf(stdout,"called %d %s. \n",mt->a,mt->b);
}
int main()
{
/* func pointer */
void (*callback)(void *);
myst m;
m.a=10;
strcpy(m.b,"123");
/* point to callback function */
callback = (void*)cbfunc;
/* perform callback and pass in the param */
callback(&m);
return 0;
}
The function pointer decleration
void (*callback)(void*);
declares a function pointer callback that takes a generic-pointer and returns void.
While assigning cbfunc to callback
callback = cbfunc;
I get the warning
warning: assignment from incompatible pointer type [enabled by default]
If I use the following
callback = (void*)cbfunc;
It compiles without any warnings.I am using gcc (4.6.3) and compiling with CFLAGS=-g -O3 -Wall
Can someone explain the reason for this?