4

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! :-)

AZYZ
  • 67
  • 2
  • 1
    Possible duplicate: [Are arrays Pointers?](https://stackoverflow.com/q/3959705/11082165) / [Is an array name a pointer?](https://stackoverflow.com/q/1641957/11082165) / [Is 2d array a double pointer?](https://stackoverflow.com/q/7586702/11082165) – Brian61354270 Jul 29 '23 at 13:50
  • 1
    C++ is a typed language. You don't dereference memory, you dereference a pointer. And pointer can point to another pointer. – Yksisarvinen Jul 29 '23 at 13:51
  • 2
    Does this answer your question? [What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – Nelfeal Jul 29 '23 at 13:52
  • @closevoters Can you please explain what details or clarity is missing in this question? It's relatively basic, but seems rather clear to me. – Yksisarvinen Jul 29 '23 at 13:54
  • 3
    Why the close votes for needs details or clarity? This is a well stated question. That the OP has wrong assumptions doesn't make this question off topic. – Brian61354270 Jul 29 '23 at 13:54
  • 3
    An array is *not* a pointer, but can decay to a pointer to the first element in some circumstances. But that still doesn't make pointers and arrays the same thing. Btw; in C++ you should prefer `std::array` or `std::vector` over C-style arrays. – Jesper Juhl Jul 29 '23 at 13:56
  • 2
    Also related: [What does 'dereferencing' a pointer mean in C/C++?](https://stackoverflow.com/q/4955198/11082165) and [Why am I being told that an array is a pointer? What is the relationship between arrays and pointers in C++?](https://stackoverflow.com/q/26517164/11082165) – Brian61354270 Jul 29 '23 at 14:00
  • `A[0]` is is `int (&)[2][2]` (possibly decaying to `int (*)[2]`), `A[0][0]` is a `int (&)[2]` (possibly decaying to `int *`), `A[0][0][0]` is a `int&` (no more decay ;-) ). – Jarod42 Jul 29 '23 at 14:10
  • I voted to close for duplicate, not for lack of details or clarity. Reading the one I linked should absolutely answer this question as long as you also understand that `A[0]` and `A[0][1]` are still arrays. – Nelfeal Jul 29 '23 at 14:10
  • _"correct me if I'm wrong but in this case, `A` is a pointer to a ..."_ - No, `A` is an `int[2][2][2]`. – Ted Lyngmo Jul 29 '23 at 14:24

1 Answers1

2

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

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Sorry but you're wrong on one point. Dereferencing a pointer to an array gives you the type of the array element, so indeed you get the `int` of 1. – Mark Ransom Jul 29 '23 at 14:37
  • Thanks a lot!! It's getting way clearer now. So do I get it right that it's possible to kind of have multiple values stored in one address? So in my case at address 400 there is a value of type A[2][2][2], a value of type A[2][2], one of type A[2] and a value of int? – AZYZ Jul 29 '23 at 15:33
  • @AZYZ You can have multiple *objects* sharing the same address. Not multiple values. Similar things happen with unions and with empty base classes. Though I'm not sure how precise the standard is in each of these cases. – Nelfeal Jul 29 '23 at 15:57
  • So in my case if I print *A: so A is first converted to a Pointer to a 2-dim array and then what exactly is printed? As far as I understood dereferencing a Pointer shows the value at that address. Which according to that theory in this case should be a 2-dim array which is printed. Printing that results in converting that 2-dim array to a pointer to a 1-dim array which yields the fictional address (400) it is pointing to. Is that correct? And so the 2-dim array which resulted from *A shares the same address as the int value 3? – AZYZ Jul 29 '23 at 16:59
  • 1
    @MarkRansom `Dereferencing a pointer to an array gives you the type of the array element` That doesn't contradict my answer. `so indeed you get the int of 1.` That depends on the type of the array. – eerorika Jul 29 '23 at 19:56