简体   繁体   English

格式说明符int *警告消息

[英]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.

相关问题 如何解决这个编译警告? (格式说明符问题) - How to solve this compilation warning? (Format specifier issue) unsigned short int的格式说明符是什么? - What is the format specifier for unsigned short int? 扫描long unsigned int的格式说明符 - Format specifier for scanning long unsigned int C 中 _int8 数据类型的格式说明符 - Format specifier of _int8 data type in C 使用float格式说明符打印int变量 - Printing int variables with float format specifier printf()中的格式说明符要求一个int何时应为char - Format specifier in printf() asks for an int when should be char warning: type specifier missing, defaults to 'int' [-Wimplicit-int] main(){ 是什么意思? - What does warning: type specifier missing, defaults to 'int' [-Wimplicit-int] main(){ mean? sscanf的格式说明符:%{format%} - Format Specifier for sscanf: %{format%} 警告:字段宽度说明符 '*' 需要类型为 'int' 的参数,但参数 2 的类型为 'size_t' {aka 'long unsigned int'} - warning: field width specifier '*' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} 警告:格式'%d'期望类型为'int *',但是参数2的类型为'int **' - warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int **’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM