[英]2D array C++ pointer address
#include <stdio.h>
int main()
{ int arr2D[3][3];
printf("%d\n",((arr2D==*arr2D) && (*arr2D==arr2D[0])));
return o;
}
How the values stores at *arr2D and arr2d is same whereas arr2D is a constant pointer which will store the address of the first element and arr2D means the value present at the address which is pointed by arr2D?值如何存储在 *arr2D 和arr2d是相同的,而arr2D是一个常量指针,它将存储第一个元素的地址,而arr2D表示存在于 arr2D 指向的地址处的值?
If we draw out your array on "paper" it will look like this如果我们在“纸”上画出你的数组,它看起来像这样
+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+ | arr2D[0][0] | arr2D[0][1] | arr2D[0][2] | arr2D[1][0] | arr2D[1][1] | arr2D[1][2] | arr2D[2][0] | arr2D[2][1] | arr2D[2][2] | +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
Then you have to remember that an array naturally decays to a pointer to its first element.然后您必须记住,数组自然会衰减为指向其第一个元素的指针。 That is plain
arr2D
when a pointer is expected, is the same as &arr2D[0]
.当需要指针时,这是普通的
arr2D
,与&arr2D[0]
。
Now if we "redraw" the array, but only for arr2D[0]
(which is what is most relevant for your question) with some of the possible pointers:现在,如果我们“重绘”数组,但仅针对
arr2D[0]
(这与您的问题最相关),并带有一些可能的指针:
+-------------+-------------+-------------+-----+ | arr2D[0][0] | arr2D[0][1] | arr2D[0][2] | ... | +-------------+-------------+-------------+-----+ ^ | &arr2D[0] | &arr2D[0][0]
Since we know that arr2D
is the same as &arr2D[0]
, we can then do that substitution in the expression arr2D == *arr2D
.由于我们知道
arr2D
与&arr2D[0]
,因此我们可以在表达式arr2D == *arr2D
。 That gets us &arr2D[0] == *&arr2D[0]
.这让我们
&arr2D[0] == *&arr2D[0]
。
The dereference *
and address-of &
operators cancel each other out, so we have &arr2D[0] == arr2D[0]
.取消引用
*
和 address-of &
运算符相互抵消,所以我们有&arr2D[0] == arr2D[0]
。
Now keep up... We know that an array decays to a pointer to its first element, and we know that arr2D[0]
is an array;现在跟上... 我们知道数组衰减到指向其第一个元素的指针,并且我们知道
arr2D[0]
是一个数组; That means it will decay to &arr2D[0][0]
, leaving us with the expression &arr2D[0] == &arr2D[0][0]
.这意味着它将衰减到
&arr2D[0][0]
,留下表达式&arr2D[0] == &arr2D[0][0]
。 And as shown those two addresses are the same, which means the comparison will be true.如图所示,这两个地址是相同的,这意味着比较结果为真。
Important note: While &arr2D[0]
and &arr2D[0][0]
might both point to the same location, their types are different.重要提示:虽然
&arr2D[0]
和&arr2D[0][0]
可能都指向同一个位置,但它们的类型是不同的。 The type of &arr2D[0]
is int (*)[3]
, while &arr2D[0][0]
is of type int *
. &arr2D[0]
的类型是int (*)[3]
,而&arr2D[0][0]
的类型是int *
。
Armed with the above information it should be easy to decipher the other comparison *arr2D == arr2D[0]
, especially since all of the parts of that are already mentioned.有了上述信息,应该很容易破译另一个比较
*arr2D == arr2D[0]
,特别是因为已经提到了所有部分。
This is not valid C code.这不是有效的 C 代码。
arr2D
when used in an expression decays to a pointer to the first element, int (*)[3]
. arr2D
在表达式中使用时衰减为指向第一个元素int (*)[3]
的指针。 Whereas *arr2D
gives the first item of the 2D array, an int[3]
, which too decays when used in an expression, into an int*
.而
*arr2D
给出二维数组的第一项,一个int[3]
,在表达式中使用时也会衰减为int*
。
So the code compares an int (*)[3]
with an int*
.因此,代码将
int (*)[3]
与int*
。 They are not compatible pointer types and cannot be compared - this is a constraint violation of the standard (C17 6.5.9/2) and the compiler must produce a diagnostic message.它们不是兼容的指针类型,无法进行比较 - 这是对标准 (C17 6.5.9/2) 的约束违反,编译器必须生成诊断消息。 Meaning that this is a severe bug, and anyone bothering to try the code in a conforming C compiler would find it.
这意味着这是一个严重的错误,任何想在符合标准的 C 编译器中尝试代码的人都会发现它。
*arr2D
and arr2D[0]
are however always equivalent, per definition of the []
operator. *arr2D
和arr2D[0]
然而总是等价的,根据[]
运算符的定义。
What the code prints is anyone's guess, since code containing constraint violations is to be regarded as having undefined behavior.代码打印的是任何人的猜测,因为包含约束违规的代码将被视为具有未定义的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.