I would suggest that pointers are not a beginner topic in C++, they are mostly just a carry over from C. If you can, you should avoid them and use the STL Containers .
(我建议指针不是C ++中的新手话题,它们大多只是C的遗留物。如果可以的话,应避免使用它们并使用STL容器 。)
In your code sample, the type of arr
is int[2]
.
(在您的代码示例中, arr
的类型为int[2]
。)
You should think of that in memory as looking something like this: (您应该在内存中将其视为如下所示:)
arr --+
|
v
+---+---+
| 5 | 1 |
+---+---+
The value contained in arr
is the location of the first element (the 5
).
(arr
包含的值是第一个元素( 5
)的位置。)
arr
is essentially a pointer to that 5
. (arr
本质上是指向5
的指针。)
The only difference being that the type ( int[2]
) has also remembered how many elements there are. (唯一的区别是类型( int[2]
)还记住了有多少个元素。)
The assignment statement p = arr
works because p
's type is int*
which int[]
can decay to.
(赋值语句p = arr
起作用是因为p
的类型是int*
,而int[]
可以归为int*
。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…