喂,伙计们,为什么在声明指向指针的指针时,我们需要使用**,为什么不能只使用单个*来指向另一个指针,或者仅仅是一个语法相关的问题?
int main()
{
int a=5,*b,*c;
b=&a;
c=&b //Why cant this simply doesn't make c point to memory location of b with the above pointer declaration why is there a need to declare c as **c
}发布于 2014-11-25 07:03:16
有以下代码:
int a=5,*b,**c;
b=&a;
c=&b;我们有:
+---+
a | 5 | <-- value
+---+
|100| <-- address
+---+
+---+
*b |100| <-- value
+---+
|200| <-- address
+---+
+---+
**c |200| <-- value
+---+
|300| <-- address
+---+当您将a的地址存储在b中时,b的值就是a的地址。但是B有自己的地址(200)。C可以按值存储b的地址。但c也有自己的地址(300)。
printf("%x", &c);会给你: 300
转换*c将使您降低"1级“,并给您100 (地址200的值)。
更改**c将使您降低1级,并给您5(地址100的值)。
如果您尝试使用*c而不是**c来容纳*b,那么您如何才能一直遵循到值5呢?
在编译器上测试代码:
printf("Address of a: %x\n", &a);
printf("Address of b: %x\n", &b);
printf("Address of c: %x\n", &c);
printf("Value of a: %d\n", a);
printf("Value of b: %x\n", b);
printf("Value of c: %x\n", c); 输出:
Address of a: 28ff44
Address of b: 28ff40
Address of c: 28ff3c
Value of a: 5
Value of b: 28ff44
Value of c: 28ff40发布于 2014-11-25 06:30:00
在这种情况下
int main()
{
int a=5,*b,*c;
b=&a;
c=&b;
}在这里,b指向a,c指向b。这就是你在评论中所评论的。
C仍然指向b的内存位置。
问题是:当您取消引用时,b即*b = a = 5。
但是当你取消引用时,c,I,*c = b = &a。因此,当您取消引用c时,输出将是a的地址,而不是变量a的值。
PS :您在编译代码warning: assignment from incompatible pointer type时将面临此警告。
发布于 2014-11-25 06:17:04
你的答案只在你的问题上。
pointer到变量,使用*pointers to pointer,使用**详细信息:
**不是一个新的运算符。它是*和*的组合。在案例2中,根据你的术语,你可以想到
only single * to point a pointer to another pointer 如图所示
int * to an inother int * ==> int **编辑:
按照你的代码
int main()
{
int a=5,*b,*c;
b=&a;
c=&b;
}b是指向int的指针。您可以在那里存储int的地址,而a是一个int。太完美了。c是指向int的指针。您可以将int的地址存储在那里,b是指向int的指针。不接受。要使第2点起作用,需要将c声明为指向int *的指针,对吗?同样的表示法是int **。
https://stackoverflow.com/questions/27119944
复制相似问题