I have the following code:
int main()
{
int A[2][2][2] =
{
{{1, 2}, {3, 4}},
{{4, 5}, {5, 6}}
};
cout << A << endl; // assume it's 400
cout << *A << endl; // 400
cout << **A << endl; // 400
cout << ***A << endl; // 1
cout << &A[0][1] << endl; // 408
}
So an array obviously shares a lot of attributes with a pointer - correct me if I'm wrong but in this case, A is a pointer to a 2-dim Array of integers which points at a fictional address 400. So if I dereference once I get a pointer to a 1-dim array of size 2 still pointing at address 400.
My first question is the following: Working with "regular" pointers, dereferencing gives the value stored at the address pointed to. That's not the case for multi-dimensional arrays, right? Because the value stored at address 400 is obviously 1, not 400 (what I think could at least somehow be assumed looking at A and *A).
The second question is about the last cout in the code block. If I would type in A[0][1] I get a "pointer-to-integer type thing", right? So dereferencing once more would give me value 3 at fictional address 408. What if I reference this pointer-to-integer as I did? I know it just gives the address of the first value of the (second) 1-dim array which it is pointing to (so 408), but how do I interpret this? In memory location 408, there's obviously an integer stored (namely 3). But again, only looking at the expression &A[0][1] giving the address 408 one could think that location 408 stores a pointer to integer type thing.
I know these questions may be a bit confusing and even unspecific, but maybe someone has some words which help me with this.
Thanks! :-)
A is a pointer to a 2-dim Array of integers
Wrong. A
is a 3-dim array. It is not a pointer.
So if I dereference once I get a pointer to a 1-dim array of size 2 still pointing at address 400.
When you indirect through A
, first it implicitly converts to a pointer to first element i.e. a pointer to a 2D array. The result of indirecting through the converted pointer to 2D array is a 2D array. Indeed, the address of the first sub array is the same as the address of the outer array.
My first question is the following: Working with "regular" pointers, dereferencing gives the value stored at the address pointed to. That's not the case for multi-dimensional arrays, right?
The pointer to which an array - multi dimensional or not - converts to, behaves just like any other pointer of same type.
Because the value stored at address 400 is obviously 1
Depends on the type. The value of type A[2][2][2]
stored at the fictional address is a multi-dimensional array. It's not an integer, so it's value cannot be 1. The value of type int
at the fictional address would be 1.
If I would type in A[0][1] I get a "pointer-to-integer type thing", right?
Not quite. A[0][1]
is an array. But it can indeed implicitly convert to a pointer to integer.
What if I reference this pointer-to-integer as I did?
You didn't, because A[0][1]
isn't a pointer to integer. If you take address of A[0][1]
such as you did with &A[0][1]
, then you get a pointer to an array of 2 integers i.e. int (*)[2]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With