简体   繁体   中英

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?

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] .

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][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 . That gets us &arr2D[0] == *&arr2D[0] .

The dereference * and address-of & operators cancel each other out, so we have &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; That means it will decay to &arr2D[0][0] , leaving us with the expression &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. The type of &arr2D[0] is int (*)[3] , while &arr2D[0][0] is of type 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.

This is not valid C code.

arr2D when used in an expression decays to a pointer to the first element, 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* .

So the code compares an int (*)[3] with an 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. Meaning that this is a severe bug, and anyone bothering to try the code in a conforming C compiler would find it.

*arr2D and arr2D[0] are however always equivalent, per definition of the [] operator.

What the code prints is anyone's guess, since code containing constraint violations is to be regarded as having undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM