如何打印函数的地址? [英] How to print the address of a function?
本文介绍了如何打印函数的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我让 GCC
使用编译下面的例子 -Wall -pedantic
:
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;
}
我得到:
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
5号线做了我的变化code就像在第6行。
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?
推荐答案
这是本质上打印一个函数指针的唯一可移植的方法。
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]);
putc('\n');
这篇关于如何打印函数的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文