How to print the address of a function?

前端 未结 4 607
旧时难觅i
旧时难觅i 2020-11-29 11:18

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

#include 

int main(void)
{
  printf(\"main: %p\\n\",         


        
相关标签:
4条回答
  • 2020-11-29 11:36

    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().

    0 讨论(0)
  • 2020-11-29 11:51

    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');
    
    0 讨论(0)
  • 2020-11-29 11:54

    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.

    0 讨论(0)
  • 2020-11-29 11:55

    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.

    0 讨论(0)
提交回复
热议问题