[英]Format specifier int * warning message
I wrote simple C program with scanf & printf like: 我用scanf和printf编写了简单的C程序,例如:
int n;
scanf("%d", &n);
int result = 7 - n;
printf("%d", &result);
and got this warning message: 并收到以下警告消息:
warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=] printf("%d", &result);
警告:格式'%d'期望的参数类型为'int',但是参数2的类型为'int *'[-Wformat =] printf(“%d”,&result);
I dont understand why argument 2 has type int * instead of int? 我不明白为什么参数2具有int *类型而不是int类型? How can I solve this?
我该如何解决?
result
is a integer variable. result
是一个整数变量。 If you want to print its value then use %d
format specifier & provide the argument as only result
not &result
. 如果要打印其值,请使用
%d
格式说明符并仅作为result
不是&result
提供参数。
This 这个
printf("%d", &result);
replace with 用。。。来代替
printf("%d", result);
If you want to print the address of result
variable then use %p
format specifier. 如果要打印
result
变量的地址,请使用%p
格式说明符。
printf("%p", &result); /* printing address */
Edit : %p
format specifier needs an argument of void*
type. 编辑:
%p
格式说明符需要一个void*
类型的参数。
So to print the address of result
cast it as void*
. 因此,要打印
result
的地址,请将其void*
为void*
。 for eg 例如
printf("%p", (void*)&result); /* explicitly type casting to void* means it works in all cases */
Thanks @ajay for pointing that, I forgot to add this point. 感谢@ajay指出这一点,我忘了补充这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.