9

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.

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

oups, I knew it was something stupid like that, thanks anyway :)
5

The typedef should be:

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

Comments

5

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.