【发布时间】:2012-06-11 13:44:09
【问题描述】:
我让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;
}
我明白了:
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 行让我更改了第 6 行中的代码。
在打印函数地址时,我缺少什么来删除警告?
【问题讨论】:
-
不确定它是否对您可用,但您可以使用 register_printf_function 对define your own special format character and converter 进行调查。
-
问题不在于它“危险”。问题是转换不是由 C 语言定义的,因此不能用于符合 C 代码。你可以通过一个中间整数类型(实现定义的结果)进行转换,只要你知道存在一个可以同时保存函数和对象指针的类型)。
-
@BobJarvis 在使用选项
-Wall进行编译时,您知道如何绕过gcc抱怨新引入的转换类型字符(warning: unknown conversion type character ‘P’ in format)吗?但这也是另一个故事...... -
@R..:这是 C。我们很危险。
-
@alk:尝试在 -Wall 之后添加 -Wno-format 选项,这(如果有记忆的话)将关闭 printf/scanf 格式检查。
标签: c linux gcc function-pointers gcc-warning