C语言 格式 '%d' 需要类型为 'int' 的参数,但参数 2 的类型为 'int *'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44196725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’
提问by akanksha kumari
Each time I submit a program on hackerrank the following error occurs.
每次我在hackerrank上提交程序时都会发生以下错误。
solution.c: In function ‘main':
solution.c:22:14: warning: format ‘%d' expects argument of type ‘int', but argument 2 has type ‘int *' [-Wformat=]
printf("%d", &sum);
It would be very helpful if someone could tell me what this means?
如果有人能告诉我这是什么意思,那将非常有帮助?
回答by Marievi
I assume that you have declared sumas an int. So the correct call to printfis :
我假设您已声明sum为int. 所以正确的调用printf是:
printf("%d", sum);
as %dspecifier means that you are going to print an int, but you are passing the int's address, which is a pointer to int, or int *.
as%d说明符意味着您要打印int,但是您正在传递int的地址,该地址是指向int、 或的指针int *。
NOTE: Do not confuse printfwith scanf, as the second does require a pointer. So, for reading variable sum, you would use :
注意:不要printf与混淆scanf,因为第二个确实需要一个指针。因此,对于读取变量sum,您将使用:
scanf("%d", &sum);
but for printing, the correct way is without &, as written above.
但是对于打印,正确的方法是没有&,如上所述。
回答by Tim?tei
If you want to print the address of sum you can use printf( "%p", &sum )
如果你想打印总和的地址,你可以使用 printf( "%p", &sum )
回答by PRDeving
Int is a primitive, primitives are data stored in memory. each data chunck is set in a specific memory block, those blocks has "memory addresses" that refer to them.
int 是一个原语,原语是存储在内存中的数据。每个数据块都设置在特定的内存块中,这些块具有引用它们的“内存地址”。
If you define int i = 1your computer allocates an integer in memory (in a block, with a memory address f.e. 0xF00000) and sets its value to 1.
When you refer to this integer as i, you are accessing the value stored in 0xF00000, that happen to be 1.
如果您定义int i = 1计算机在内存中分配一个整数(在一个块中,内存地址为 fe 0xF00000)并将其值设置为 1。当您将此整数称为 时i,您正在访问存储在 0xF00000 中的值1.
In C you can also get the ireference (the memory address it's allocated in) by prefixing it with & (ampersand), by doing this you will get the memory address of the variable rather than its value.
在 C 中,您还可以通过在i引用前加上 &(与号)来获取引用(分配给它的内存地址),通过这样做,您将获得变量的内存地址而不是它的值。
i === 1; // true
&i === 1; //false
&i === 0xF00000; //true
This memory address can be assigned to a pointer (a variable that 'points' to a memory address, thus, have no it's own value) so it can be accessed directly too dereferencing itso you can gather the value inside that memory block. This is achieved using *
可以将此内存地址分配给一个指针(一个“指向”内存地址的变量,因此没有它自己的值),因此可以直接访问它,也可以取消引用它,以便您可以收集该内存块内的值。这是使用*
int i = 1; //this allocates the
int *ptr = &i; //defines a pointer that points to i address
/* now this works cause i is a primitive */
printf("%d", i);
/* this works also cause ptr is dereferenced, returning the
value from the address it points, in this case, i's value */
printf("%d", *ptr);
In your example, you are passing a reference to printf (printf asks for a value and is receiving a memory address) so it doesnt work.
在您的示例中,您正在传递对 printf 的引用(printf 要求一个值并正在接收一个内存地址),因此它不起作用。
Hope this helps you understand C and pointers better
希望这可以帮助您更好地理解 C 和指针

