C cast void pointer to function pointer

The C99 standard does not allow to convert between pointers to data (in the standard, “objects or incomplete types” e.g. char* or void*) and pointers to functions.

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; the result shall
compare equal to the original pointer. If a converted pointer is used
to call a function whose type is not compatible with the pointed-to
type, the behavior is undefined.

One reason is that pointers to objects and pointers to functions do not have to be the same size. On an example architecture, the former can be 64-bit and the latter 32-bit.

You can cast from a pointer to a certain function type to a pointer to another function type, and this is the technique I recommend you use if you need to store function pointers in a data structure. Any function type will do. This means you cannot reuse a data structure intended for data pointers. You need to duplicate the data structure and change it to hold function pointers.

Do not forget to cast back to the proper function pointer type before calling, otherwise this is undefined behavior.

NOTE that, as pointed out by Andrew Mellinger, some compilers allow the conversion in each direction. C11’s annex “J.5 Common extensions” lists:

J.5.7 Function pointer casts

1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to be invoked as a function (6.5.4).

2 A pointer to a function may be cast to a pointer to an object or to void, allowing a function to be inspected or modified (for example, by a debugger) (6.5.4).

Some POSIX interfaces, such as dlsym(), also in effect mandate these conversions to be valid in the POSIX system’s C compiler.

Leave a Comment