Re: Casting function pointers



KKL wrote:

Ok, so I accept my offense that the code snippets have syntactical
issues and that is because I did not simply copy it from my C source
files but just wrote it in hurry to put up an example for my point of
discussion which was "IS TYPE CASTING FUNCTION POINTERS FROM void (*)
(int) TO (void *) AND BACK TO void (*)(void) AND THEN USING IT TO
CALL FUNCTIONS IS A VALID AND SAFE OPERATION OR NOT".

According to the C99 standard, paragraph 6.3.2.3p8:

"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."

In other words, it is safe to convert them, as long as you convert them back to the original type before calling the function. If you call them as a different type, the behavior is undefined (potentially crashing your program)
.



Relevant Pages