在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;
为什么这两个操作符在其功能上不对称?
ptr_a =&a
表示ptr_a指向a。但是,&&a
->没有“指针”可以指向的指针。ptr_a是堆栈上的一些内存,因此您可以将其指向。 - nothrowint *ptr_ptr_a = &ptr_a;
就可以。 - karlphillip