这可能是架构问题。我在 OS X 上运行它并且运行良好:
#import <stdio.h>
int main()
{
int a;
printf("&a = %u\n",(unsigned int)&a);
printf("a\n");
printf("b\n");
printf("c\n");
printf("d\n");
}
但是,我在编译过程中确实收到了警告:
warning: cast from pointer to integer of different size
我在 64 位机器上,所以我的 64 位指针无法放入 32 位 int。尝试运行它,看看你得到了什么:
#import <stdio.h>
int main()
{
int a;
printf("unsigned %ld\n", sizeof(unsigned)); // sizes reported in bytes
printf("int %ld\n", sizeof(int)); // same as unsigned
printf("long %ld\n", sizeof(long)); // should fit a pointer
printf("ptr %ld\n", sizeof(void*)); // size of a pointer
printf("&a = %lu\n", (unsigned long)&a); // should print out your pointer
printf("&a = %p\n", &a); // the Right Way of doing things
}
输出:
unsigned 4
int 4
long 8
ptr 8
&a = 140734544742408
&a = 0x7fff508c0808
编辑:
以防万一你不知道,unsigned
实际上意味着unsigned int
。同样,long
实际上意味着long int
.
此外,一个值是否signed
会unsigned
改变它的物理大小,只有它是如何被解释的。您可以从前两张照片中看到这一点。