ZU C: void* Zeiger auf Funktionenzeiger casten?
-
Hallo
möchte gerne wissen wie folgendes funktioniert.Am besten ein Beispiel:
int (*funktion)(const char*, ...) = printf;
void* zeiger = funktion; //bzw void* zeiger = printf;//Wie kann ich jetzt zeiger ansprechen bzw. wie sieht cast aus.
*((cast)zeiger)("Hallo Du");
Geht das überhaupt?
Vielen Danke schonmal fürs anschauen.
Klirxel
-
void *'s are only guaranteed to hold object (i.e. data) pointers; it is not portable to convert a function pointer to type void *. (On some machines, function addresses can be very large, bigger than any data pointers.)
It is guaranteed, however, that all function pointers can be interconverted, as long as they are converted back to an appropriate type before calling. Therefore, you can pick any function type (usually int ()() or void ()(), that is, pointer to function of unspecified arguments returning int or void) as a generic function pointer. When you need a place to hold object and function pointers interchangeably, the portable solution is to use a union of a void * and a generic function pointer (of whichever type you choose).
-
Wenn, dann sowieso &printf, oder?
-
wxSkip schrieb:
Wenn, dann sowieso &printf, oder?
Ist bei Funktionen gleichbedeutend. Den Adressoperator & kannste Dir sparen.