有没有办法在ANSI C中打印指向函数的指针?当然这意味着你必须将函数指针强制转换为void指针,但似乎不可能?
#include <stdio.h>
int main() {
int (*funcptr)() = main;
printf("%p\n", (void* )funcptr);
printf("%p\n", (void* )main);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ gcc -ansi -pedantic -Wall test.c -o test
test.c:在函数'main'中:
test.c:6:警告:ISO C禁止将函数指针转换为对象指针类型
test.c:7:warning :ISO C禁止将函数指针转换为对象指针类型
$ ./test
0x400518
0x400518
它"工作",但非标准...
任何人都可以告诉下面的代码有什么问题吗?
int main () {
return main();
}
Run Code Online (Sandbox Code Playgroud)
我测试过,它编译正确.它一直在运行.现场背后的诀窍呢?
为什么要在我的电脑上cout<<main;打印1?我以为它会打印主要的地址.但是当我使用时,printf("%x",main);我会得到不同的输出.
编辑:
我尝试std::cout过其他功能.我找到1了每一个案子.