ISO C Void * and Function Pointers

Tomwa ivory picture Tomwa ivory · Jan 3, 2013 · Viewed 12k times · Source

While following some tutorials and reading about function pointers I learned that evidently assigning a void pointer to a function pointer in ISO C is undefined, is there any way to resolve the warning I receive during compile time (e.g. a better way of coding it) or should I just ignore it?

Warning:

ISO C forbids assignment between function pointer and 'void *' [-pedantic]

Example Code:

void *(*funcPtr)();
funcPtr = GetPointer();

GetPointer is a function that returns a void pointer E.G.

void *GetPointer();

Answer

Weinan Li picture Weinan Li · Oct 21, 2013

In tlpi-book I found this trick very interesting:

#include <dlfcn.h>

int
main(int argc, char *argv[])
{
    ...
    void (*funcp)(void);        /* Pointer to function with no arguments */
    ...
    *(void **) (&funcp) = dlsym(libHandle, argv[2]);
}