I am trying to define a struct type (call it thing) that has a function pointer in it (call it bar), and I want that function to take an argument that is a pointer to an object of type thing.
I know from here that I can either do a forward declaration or declare the struct type in the argument list of the function pointer as shown here:
// In header
typedef struct thing thing_type;
typedef struct
{
int a;
int b;
double (*bar)(thing_type *t)
} thing;
OR
// In header
typedef struct
{
int a;
int b;
double (*bar)(struct thing *t)
} thing;
But when I go to initialize these values, I always get an incompatible pointer type warning.
double bar1(thing *t);
double bar2(thing *t);
// In main()
thing foo = {1, 2, bar1}; // WARNING
initialization of ‘double (*)(struct thing *)’ from incompatible pointer type ‘double (*)(thing *)’ [-Wincompatible-pointer-types]
foo.bar = bar2; // WARNING
passing argument 1 of ‘foo->bar’ from incompatible pointer type [-Wincompatible-pointer-types]
My code works the way I intend it to as far as I can tell through testing, but I would like to understand if I am doing anything "wrong" here or if there is a way to do what I want without throwing warnings for every assignment.
Note that thing foo = {1, 2, &bar1 }; (passing the address of a function pointer) makes no difference for this warning.