我感兴趣的是,unsigned long long
变量是否可以被视为地址,结果似乎是正确的?
int var = 0;
// the variable var's address is 0x7fffffffe4a4 now in my system
unsigned long long ull = 0x7fffffffe4a4;
scanf("%d", ull); // I try to use ull as var's address and assign a value to var
printf("%d\n", var); // It seemingly works!
我还做了一个实验,具体如下.
int var = 0;
// the variable var's address is 0x7fffffffe4a4 now in my system
unsigned long long ull = 0x7fffffffe4a4;
int *ptr = &var;
// First, I want to know whether the memory used by unsigned long long and the one used by a pointer differ
printf("%zu\n", sizeof(ull));
printf("%zu\n", sizeof(ptr));
// The results are both "8" in my system
// Next I try to assign two different values to var by two ways
scanf("%d", ull); // I try to use x as an address to assign var
printf("%d\n", var);
scanf("%d", ptr); // And I also use a normal pointer to assign var
printf("%d\n", var);
// The results were in line with expectations!
看起来unsigned long long
变量可以成功地用作指针(尽管编译器警告我参数应该是int*
而不是unsigned long long
),我想知道…
在硬件级别上,变量和指针有什么不同?这两种类型的对象在存储和使用时是如何处理的?由谁处理这些对象?是否建议执行上述操作?
(归根结底,这是c的一个特性吗?)