c - How to format a function pointer? -
is there way print pointer function in ansi c? of course means have cast function pointer void pointer, appears that's not possible??
#include <stdio.h> int main() { int (*funcptr)() = main; printf("%p\n", (void* )funcptr); printf("%p\n", (void* )main); return 0; }
$ gcc -ansi -pedantic -wall test.c -o test
test.c: in function 'main':
test.c:6: warning: iso c forbids conversion of function pointer object pointer type
test.c:7: warning: iso c forbids conversion of function pointer object pointer type
$ ./test
0x400518
0x400518
it's "working", non-standard...
the legal way access bytes making pointer using character type. this:
#include <stdio.h> int main() { int (*funcptr)() = main; unsigned char *p = (unsigned char *)&funcptr; size_t i; (i = 0; < sizeof funcptr; i++) { printf("%02x ", p[i]); } putchar('\n'); return 0; }
punning function pointer void *
, or non character type, dreamlax's answer does, undefined behaviour.
what bytes making function pointer mean implementation-dependent. represent index table of functions, example.
Comments
Post a Comment