This question already has answers here:
Closed 3 years ago.
Correct format specifier to print pointer or address?
(4个答案)
上面的代码应该打印出包含整数3的内存块的地址(无符号整数)。但我却发现了这个错误-
我从各种渠道证实:
一。
2。
三。j包含一个无符号int值,它是j所指向的内存块的地址。
(4个答案)
#include <stdio.h>
int main(void)
{
int i = 3;
int* j = &i;
printf("%u",j);
}
上面的代码应该打印出包含整数3的内存块的地址(无符号整数)。但我却发现了这个错误-
error: format specifies type 'unsigned int' but the argument has type 'int *'
。我从各种渠道证实:
一。
*j
是指“存储在j中的地址的值”2。
&j
指存储指针j的内存块的地址。三。j包含一个无符号int值,它是j所指向的内存块的地址。
最佳答案
您的%u
不正确。j
是指针,因此应该使用%p
。这一改变应做到:
printf("%p", (void *)j);
关于c - 如何获得指针指向的内存位置的地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38811347/