C function pointer casting to void pointer

The C standard does not allow to cast function pointers to void*. You may only cast to another function pointer type. In the C11 standard, 6.3.2.3 §8:

A pointer to a function of one type
may be converted to a pointer to a
function of another type and back
again

Importantly, you must cast back to the original type before using the pointer to call the function (technically, to a compatible type. Definition of “compatible” at 6.2.7).

Note that the POSIX standard, which many (but not all) C compilers have to follow too because of the context in which they are used, mandates that a function pointer can be converted to void* and back. This is necessary for some system functions (e.g. dlsym).

Leave a Comment