在 C 中获取地址的地址是唯一的方法吗(与双重解引用相反,有一个中间变量?
例如我有:
int a;
int b;
int *ptr_a;
int *ptr_b;
int **ptr_ptr_a;
a = 1;
ptr_a = &a;
ptr_ptr_a = &(&a); <- compiler says no
ptr_ptr_a = &&a; <- still compiler says no
ptr__ptr_a = &ptr_a; <- ok but needs intermediate variable
但你可以反过来,例如
b = **ptr_ptr_a; <- no intermediate variable required
例如我不必做:
ptr_b = *ptr_ptr_b;
b = *ptr_b;
为什么这两个运算符的功能不对称?
最佳答案
address-of 运算符返回一个右值,您不能获取右值的地址(请参阅 here 以了解右值和左值之间的区别)。
因此,您必须通过将其存储在变量中将其转换为左值,然后才能获取该变量的地址。
关于变量地址的C地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9976586/