This warning seems to not be necessary. In my code, I have a function pointer type
(void*)(*function_pointer)(void* data)
and some functions require function pointers of this type as a parameter, because they do not depend on the return type, it just has to be a pointer.
But, when I pass a function like somestruct* function_A(void* data)
to it, I get that warning, even though somestruct* could be implicity converted to void*, the same way that I can pass to a function int function_test(void* data) a somestruct pointer and it would work without warnings.
Yes I can define function_A with a void * as return type, but it would mean everyone who will use that function for other reasons will have to cast its return value to somestruct *. I have these two options: leave as it is and ignore the warning which annoys me a bit, or change the function definition, which would annoy me even more.
So here I am, asking if there is a third option.
It is allowed to convert any object pointer to a
void *and back, however that does not carry over to arguments and return types in function pointers.A pointer to function
somestruct* function_A(void*)is not compatible with a function pointer of typevoid* (*function_pointer)(void*)because the return types are not compatible. Attempting to call a function through an incompatible pointer type will trigger undefined behavior.The rules regarding how to determine if two function types are compatible are spelled out in section 6.7.6.3p15 of the C standard:
The portion in bold is what's relevant in this particular case. A
void *and and struct pointer are not compatible, even though it's allowed to convert between the two.The rules of function pointer conversions are spelled out in section 6.3.2.3p8:
With the last section in bold stating that what you're attempting to do is not allowed.
You'll need to change the return type of your function to
void *to be compatible with the function pointer type. Casting the return type however is not necessary, as conversions between an object pointer and avoid *is allowed without one.