Reputation: 10712
This is actually a non-critical question, but I get this warning most of the time I use function pointers and still couldn't figure out why on my own. Consider the following prototype:
typedef void * Pointer;
void tree_destroyLineage(Tree greatest_parent, void *dataDestructor(Pointer data));
And so far I can compile my thousand-line-long code and get zero warnings. So I'm assuming I wrote the declaration correctly. But then I call it in code, passing free as my destructor, since the data stored in the tree nodes are simple structs:
tree_destroyLineage(decision_tree, free);
And this makes me get a "warning: passing argument 2 of 'tree_destroyLineage' from incompatible pointer type"
message. My first hypotesis was that the compiler couldn't figure out at compile time that Pointer
and void *
are the same thing, so I tried both creating another function with the exact same types of the function pointer that "repasses" the call to free()
and changing the function pointer declaration to accept a void *
instead of a Pointer. Both approaches gave me the very same warning at the very same place.
What am I doing wrong and how do I solve it?
Upvotes: 2
Views: 739
Reputation: 211
I'm not sure about your library, but my free has no return value (i.e., it's void). It doesn't return a void pointer.
If you want the 2nd argument to be a pointer to a function which returns void and takes as an argument a void pointer, I believe what you want is:
void (*fn)(void*)
and not
void *fn(void*)
which is what you have.
Upvotes: 7
Reputation: 26998
Try this:
void tree_destroyLineage(Tree greatest_parent, void (*dataDestructor)(void *data));
Using a typedef
for a void pointer is just plain silly.
Edit: the problem is that, without the parenthesis around *dataDestructor
, the compiler thinks that the function returns void *
, rather than void
. The parenthesis tell the compiler that the function returns void
, but is a pointer to a function.
Upvotes: 2
Reputation:
void tree_destroyLineage(Tree greatest_parent,
void *dataDestructor(Pointer data));
should be:
void tree_destroyLineage(Tree greatest_parent,
void (*dataDestructor)(Pointer data));
Upvotes: 4
Reputation: 46773
I believe the correct signature for a function like free is:
void (*freefunc)(void*)
not
void *dataDestructor(Pointer data)
Upvotes: 10