How to print the address of a function?

你离开我真会死。 提交于 2019-11-26 14:35:33

问题


I let gcc compile the following example using -Wall -pedantic:

#include <stdio.h>

int main(void)
{
  printf("main: %p\n", main); /* line 5 */
  printf("main: %p\n", (void*) main); /* line 6 */

  return 0;
}

I get:

main.c:5: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int (*)()’
main.c:6: warning: ISO C forbids conversion of function pointer to object pointer type

Line 5 made my change the code like in line 6.

What am I missing to remove the warning when printing a function's address?


回答1:


This is essentially the only portable way to print a function pointer.

size_t i;
int (*ptr_to_main)() = main;
for (i=0; i<sizeof ptr_to_main; i++)
    printf("%.2x", ((unsigned char *)&ptr_to_main)[i]);
putchar('\n');



回答2:


While converting a function pointer to a void pointer is technically dangerous, converting function pointers to void pointers is used in the POSIX standard, so it is almost sure to work on most compilers.

Look up dlsym().




回答3:


It's right there in the warning: ISO C forbids conversion of a function pointer to an object pointer type, which includes void*. See also this question.

You simply can't print the address of a function in a portable way, so you can't get rid of the warning.

You can print a function pointer using @R..'s suggestion.




回答4:


This whole idea is indeed non-portable, since some systems use different sized pointers to code and data.

What you really need is platform-specific knowledge of how big a function pointer is, and a cast to an integral type of that size. Unfortunately, I don't think anyone has standardized a intfuncptr_t analagous to intptr_t which can hold any data pointer.


As R. notes in his answer, you can always treat the pointer as an array of (possibly signed or unsigned) char, this way you don't need any integral type of the correct size.



来源:https://stackoverflow.com/questions/10932910/how-to-print-the-address-of-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!