eotcl said:
Dear Group!
Is it possible to dereference the void pointer to function pointer,
and refer to this pointer later as if it was a function.
I believe that the term you're looking for is "convert", not
"dereference". The standard allows a null pointer of any type to be
converted into a function pointer, but it becomes a null pointer of the
function pointer type, which cannot be used to call a function. The
standard does not describe any other way in which a void* can be
converted to a function pointer type, though many implementations allow
this as an extension.
Note that any pointer to a function type can be converted to any other
pointer to function type, and back again; the resulting pointer value is
equivalent to the original. Therefore, you can use any particular
pointer to function type as a generic pointer to function type, in the
same sense that void* is a generic pointer to object type. I generally
use void(*)() for such purposes, because its the absolute minimally
complicated pointer type.
If you need to use a single object that can point to either objects or
functions, use an object of the following type:
union generic_pointer {
void *pobj;
void (*pfunc)();
};
As with all unions, make sure that you keep track of which member of the
union is currently in use. Use the wrong member, and very bad things
could happen. However, this is no different from the rule for void*
itself: you must somehow keep track of which type it should be converted
back to.