C : function pointer and typedef problem – Education Career Blog

I have a C function that takes a function pointer as argument, it’s a destructor that I’ll call at the end of my program. Here is the prototype of my function :

int store_dest(void (*routine)(void *));

I want to store this function pointer into a structure with some other infos. In order to have a nice struct, I want to have a typedef to this function pointer. Here is my typedef :

typedef void (*my_destructor)(void *);

And here is my structure :

typedef struct my_struct{
  my_destructor dest;
  other_info ...
} my_struct;

During the initialization step, I want to set the “dest” field to a function of mine, here is the prototype of this function :

void* my_dummy_dest(void* foo);

The problem (in fact it’s just a warning but I’d like to suppress it) occurs when I try to set the “dest” field of my structure to “my_dummy_dest” :

my_struct.dest = &my_dummy_dest;

I get a “warning: assignment from incompatible pointer type”

Same when I just compare them :

if (my_struct.dest == &my_dummy_dest)

I get a “warning: comparison of distinct pointer types lacks a cast”

But I get no warning when I set the “dest” field with another routine. I don’t get why I have those warnings.

,

Your dummy destructor is declared to return a void pointer, not a void. This declaration does not match the typedef for your destructor function pointer.

,

The typedef should be:

typedef void *(*my_destructor)(void *);

,

Because my_dummy_test returns void*
instead of void. If you want to return void*, the typedef should be

typedef void* (*my_destructor)(void *);

Leave a Comment