[英]Pointers to pointer
Hello guys can someone explain why while declaring pointers to pointer we need to use **
why cant we use only single *
to point a pointer to another pointer or is it just a syntax related issue Eg 大家好,有人可以解释为什么在声明指向指针的指针时需要使用**
为什么不能仅使用单个*
来将指针指向另一个指针,还是仅仅是语法相关的问题?
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
}
In this case 在这种情况下
int main()
{
int a=5,*b,*c;
b=&a;
c=&b;
}
Here b
points to a
and c
points to b
. 这里b
指向a
, c
指向b
。 It is what you have commented in the commented. 这就是您在评论中发表的评论。
c still points to the memory location of b. c仍指向b的存储位置。
The catch is : When you de-reference b
ie *b = a = 5
. 要注意的是:当取消引用b
即*b = a = 5
。
But When you de-reference c
ie *c = b = &a
. 但是当您取消引用c
即*c = b = &a
。 So When you dereference c
the output would be address of a instead of the value of the variable a 因此,当取消引用c
,输出将是c
的地址,而不是变量a的值。
PS : you will face this warning when compiling the code warning: assignment from incompatible pointer type
PS:编译代码warning: assignment from incompatible pointer type
时,您将面临此warning: assignment from incompatible pointer type
With the following codes: 使用以下代码:
int a=5,*b,**c;
b=&a;
c=&b;
We have: 我们有:
+---+
a | 5 | <-- value
+---+
|100| <-- address
+---+
+---+
*b |100| <-- value
+---+
|200| <-- address
+---+
+---+
**c |200| <-- value
+---+
|300| <-- address
+---+
When you store a's address in b, b's value is a's address. 当您将a的地址存储在b中时,b的值就是a的地址。 But b has it's own address (200). 但是b有它自己的地址(200)。 c can store b's address as it's value. c可以存储b的地址作为其值。 But c has it's own address too (300). 但是c也有它自己的地址(300)。
printf("%x", &c);
will give you: 300 会给你:300
Deferencing *c will get you down "1 level" and give you 100 (get value of address 200) 延缓* c将使您降低“ 1级”并给您100(获取地址200的值)
Deferencing **c will get you down 1 more level and give you 5 (get value of address 100) 防御** c将使您再降低1级,并给您5(获得地址100的值)
If you try to use *c instead of **c to hold *b, how are you able to deference all the way down to reach value 5? 如果您尝试使用* c而不是** c来持有* b,那么如何才能一路向下推至值5?
Testing the codes on a compiler: 在编译器上测试代码:
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);
Output: 输出:
Address of a: 28ff44
Address of b: 28ff40
Address of c: 28ff3c
Value of a: 5
Value of b: 28ff44
Value of c: 28ff40
You have your answer in your question only. 您只有在问题中有答案。
pointer
to variable , use of *
pointer
变量的pointer
,使用*
pointers to pointer
of a variable , use **
pointers to pointer
变量pointers to pointer
,使用**
Details: 细节:
**
is not a new operator. **
不是新运营商。 it's a combination of *
and *
. 它是*
和*
的组合。 In case 2. as per your terminology, you can think of 在情况2中,根据您的术语,您可以想到
only single * to point a pointer to another pointer
as in 如
int * to an inother int * ==> int **
EDIT: 编辑:
as per your code 根据您的代码
int main()
{
int a=5,*b,*c;
b=&a;
c=&b;
}
b
is a pointer to int
. b
是指向int
的指针。 You can store the address of int
there, and a
is an int
. 您可以在其中存储int
的地址,而a
是int
。 Perfect. 完善。 c
is a pointer to int
. c
是指向int
的指针。 You can store the address of int
there, and b
is a pointer to int
. 您可以在其中存储int
的地址, b
是指向int
的指针。 Not accepted. 不接受。 To make point 2 work, you need to declare c
as a pointer to int *
, right? 为了使第2点正常工作,您需要将c
声明为指向int *
的指针,对吗? The notation for the same is int **
. 相同的表示法是int **
。
Every level of indirection needs a level of dereferencing. 每个间接级别都需要一个取消引用级别。 So for: 因此对于:
T*** x = ...;
you would need: 您将需要:
***x
to get to T&
. 去T&
。
If you had a pointer to pointer and you saved it in: 如果您有一个指向指针的指针,并将其保存在以下位置:
T* x = ...;
T* y = &x;
it would mean that *ptr
leads to T&
, while it really leads to another T*
. 这意味着*ptr
会导致T&
,而实际上会导致另一个T*
。
Here's another way to think of pointers-to-pointers: imagine how it works in memory. 这是思考指针到指针的另一种方式:想象一下它如何在内存中工作。 Here's a little snippet that shows what I mean: 这是显示我的意思的小片段:
int TheInteger = 123;
int *p = &TheInteger;
int **pp = &p;
printf("The value at memory location &pp (0x%x) is 0x%x (pp). This value (which we assigned as &p (0x%x) is 0x%x (p). This value, in turn, we assign as &TheInegeter (0x%x) points to the 'instance' of TheInteger, which is %d", &pp, pp, &p, p, &TheInteger, TheInteger);
The output of this would be: 输出为:
The value at memory location &pp (0x657e588) is 0x657e594 (pp). This value (which we assigned as &p (0x657e594) is 0x657e5a0 (p). This value, in turn, we assign as &TheInegeter (0x657e5a0) points to the 'instance' of TheInteger, which is 123
Now, to go back to your original question, you cannot declare a variable as being a pointer when the value you're setting it to is a pointer-to-a-pointer. 现在,回到您的原始问题,当您将变量设置为指针的值时,您不能将变量声明为指针。 In other words, in your example, you set 'b' as a pointer to a -- so, you can't tell the compiler that 'c' is just a pointer and then try to set it to a value that the compiler knows is a pointer-to-a-pointer. 换句话说,在您的示例中,您将'b'设置为指向a的指针-因此,您无法告诉编译器'c'只是一个指针,然后尝试将其设置为编译器知道的值是一个指向指针的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.