我试着玩一些指针来指向'i'的指针,我发现有两个不同的地址分配给声明%u和%lu,%llu。怎么可能是一个变量可以在执行相同的实例有两个不同的地址 -变量如何在同一时间点有两个不同的地址?
#include <stdio.h>
int main(void)
{
int i;
float f;
printf("\nEnter an integer:\t");
scanf("%d",&i);
printf("\nValue of address of i=%u",&i);
printf("\nvalue of address of i=%d",&i);
printf("\nValue of address of i=%lu",&i);
printf("\nValue of address of i=%llu",&i);
printf("\nvalue of i=%d",i);
printf("\nvalue of i=%u",i);
printf("\nvalue of i=%lu",i);
printf("\nvalue of i=%llu\n",i);
}
这是输出 -
[email protected]:~/Desktop/Daily programs/pointers$ ./pointer001
Enter an integer: 12
Value of address of i=1193639268
value of address of i=1193639268
Value of address of i=140725797092708
Value of address of i=140725797092708
value of i=12
value of i=12
value of i=12
value of i=12
在这里,我们可以清楚地看到,%u和%d的地址是1193639268(尽管%d和%u的输出在所有情况下可能不相等),%lu和%llu的输出是140725797092708,它的物理意义是什么。
细节:用于打印'void *'指针的适当格式说明符是'“%p”'。 ''%p“'可能不适用于'int *',但常见。 – chux
@chux好点。我已经添加了这个细节 – dbush