指向指针的指针
Pointers to pointer
大家好,有人可以解释为什么在声明指向指针的指针时我们需要使用 **
为什么我们不能只使用单个*
将指针指向另一个指针,或者这只是一个与语法相关的问题,例如
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
}
使用以下代码:
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: %xn", &a);
printf("Address of b: %xn", &b);
printf("Address of c: %xn", &c);
printf("Value of a: %dn", a);
printf("Value of b: %xn", b);
printf("Value of c: %xn", 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
在这种情况下
int main()
{
int a=5,*b,*c;
b=&a;
c=&b;
}
这里的b
指向a
,c
指向b
。这是您在评论中评论的内容。
C 仍然指向 B 的内存位置。
问题是:当你取消引用b
即*b = a = 5
.
但是当你取消引用c
即*c = b = &a
.因此,当您取消引用c
输出将是 a 的地址而不是变量 a 的值
PS :编译代码时,您将面临此警告warning: assignment from incompatible pointer type
每个间接寻址级别都需要一个级别的取消引用。所以对于:
T*** x = ...;
您将需要:
***x
去T&
.
如果您有指向指针的指针并将其保存在:
T* x = ...;
T* y = &x;
这意味着*ptr
会导致T&
,而它实际上会导致另一种T*
。
你的答案只在你的问题中。
-
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 **
.
这是另一种思考指针到指针的方法:想象一下它在内存中是如何工作的。 这里有一个小片段来说明我的意思:
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 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
现在,回到最初的问题,当您设置变量的值是指针到指针时,您不能将变量声明为指针。 换句话说,在您的示例中,您将"b"设置为指向 -- 的指针,因此,您无法告诉编译器"c"只是一个指针,然后尝试将其设置为编译器知道是指针到指针的值。
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错