How to print the address of a function?

前端 未结 4 618
旧时难觅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: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.

提交回复
热议问题