alk*_*alk 16 c linux gcc function-pointers gcc-warning
我gcc使用以下方法编译以下示例-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;
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
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
Run Code Online (Sandbox Code Playgroud)
第5行让我更改了第6行中的代码.
在打印功能的地址时删除警告我错过了什么?
R..*_*R.. 16
这实际上是打印函数指针的唯一可移植方式.
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');
Run Code Online (Sandbox Code Playgroud)