打印功能地址

4 c pointers function-pointers function

我一直试图弄清楚如何打印函数的地址,这就是我想出来的

#include<stdio.h>
int test(int a, int b)
{
     return a+b;
}
int main(void)
{
     int (*ptr)(int,int);
     ptr=&test;
     printf("The address of the function is =%p\n",ptr);
     printf("The address of the function pointer is =%p\n",&ptr);
     return 0;
}
Run Code Online (Sandbox Code Playgroud)

它没有任何警告和错误

address of function is =0x4006fa
address of function pointer  is =0x7fffae4f73d8
Run Code Online (Sandbox Code Playgroud)

我的问题是否使用%p格式说明符是打印函数地址的正确方法还是有其他方法可以这样做?

M.M*_*M.M 5

这是不正确的.%p仅适用于对象指针类型(事实上,void *具体而言).函数指针没有格式说明符.