I have the following code:
long fp = ...
void (*ptr)(long, char*, char*) = fp;
The long fp
is a correct function pointer, which comes in as a long. I am getting the standard "makes pointer from int without a cast" warning. I want to be able to compile with:
-std=iso9899:1990 -pedantic-errors
which turns that warning into an error. The question is: what is the correct cast? I have tried various guesses, e.g.:
void (*ptr)(long, char*, char*) = (void)(*)(long, char*, char*) fp;
But can't seem to find the right one.
The "correct" cast is:
void (*ptr)(long, char*, char*) = (void (*)(long, char*, char*))fp;
Obviously, this can be tidied up with a suitable typedef.
But either way, the result of this is implementation-defined. If you can, avoid this, and maintain the pointer in a pointer type. Next best thing would be to use intptr_t
if it's available.
Probably it's something like:
void (* ptr)(long, char, char *) = (void (*)(long, char, char *))fp;
but my suggestion is to use a typedef
and forget about all this mess:
typedef void (* fnPtr)(long, char, char*);
fnPtr ptr = (fnPtr) fp;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With