-1

I was learning about 2D arrays and pointer representation of it and all. I ran the following to get the internal working of 2d arrays. I got strange results for the arr2d[0]

    int arr2d[2][3]{{1,2,3},{4,5,6}};
    cout<<"&arr2d[0][0] :"<<&arr2d[0][0]<<endl;
    cout<<"(&(arr2d[0])) :"<<(&(arr2d[0]))<<" arr2d[0] :"<<arr2d[0]<<endl;
    cout<<"*(arr2d[0]) :"<< *(arr2d[0])<<" *(&(arr2d[0])) :"<<*(&(arr2d[0]))<<endl;

output:

&arr2d[0][0] :0x3e6d7ff6c0
(&(arr2d[0])) :0x44efbffbe0 arr2d[0] :0x44efbffbe0
*(arr2d[0]) :1 *(&(arr2d[0])) :0x44efbffbe0

How can *(arr2d[0]) have 1 as output, if arr2d[0] is "circularly pointing" itself? Also apparently pointer arthimetic is not functioning for arr[0], as arr[0]+1 doesn't gets one to arr[1].

I was not expecting the arr[0] to be circularly pointing to itself, I expected it to a pointer to a 1D array of length 3. this tells that this happens but I want to know why how can arr[0] point to itself but at the same time *(arr[0]) gives 1?

  • what do you mean with "point to itself" ? The whole array, the first row and the first element all have the same adress – 463035818_is_not_an_ai Jan 10 '23 at 17:54
  • An array in C++ is merely a sequence of its elements, contiguous in memory. An array of `int[3]` is a sequence of `int[3]` in memory, and `int[3]` is itself a sequence of `int` in memory. Dereferencing removes one layer of that and is fairly similar to just getting the zero'th element to begin with. In fact, in the absence of possible alignment issues and other messy memory details, `int[2][3]` is basically the same thing as `int[6]`. – Silvio Mayolo Jan 10 '23 at 17:56
  • 2
    details matter, the output of the code you posted does not start with `12345` – 463035818_is_not_an_ai Jan 10 '23 at 17:56
  • 2
    Side note: Many people are taught at an array is a pointer to its first element. This is not true. An array is an array. it simply converts, [decays](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay), to a pointer easily and automatically. – user4581301 Jan 10 '23 at 18:01
  • 2
    Very related: [What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay?r=Saves_AllUserSaves) – πάντα ῥεῖ Jan 10 '23 at 18:04
  • 2
    This output cannot be produced by this code. Please post a [mcve]. – n. m. could be an AI Jan 10 '23 at 18:08
  • that 12345 is a system generated gibberish output, printed with every file, but I get what youre saying. Shouldn't * (arr) and *(&arr) be giving same value given that arr and &arr have same value in here. – Shashank Rana Jan 10 '23 at 18:14

1 Answers1

2

How can *(arr2d[0]) have 1 as output, if arr2d[0] is "circularly pointing" itself?

It's not pointing to itself. Arrays are not pointers, but like any variable they do have an address, and the address of an array is the same as the address of the first element. This extends to multidimensional arrays, so arr2d, arr2d[0], and arr2d[0][0] all have the same address, although their types are different.

apparently pointer arthimetic is not functioning for arr[0], as arr[0]+1 doesn't gets one to arr[1].

arr2d[0] decays to &arr2d[0][0], so aarr2drr[0]+1 is &arr2d[0][0]+1 which is the same as arr2d[0][1]. Had you used the expression arr2d + 1, that would be the same as &arr2d[0] + 1 which gives you arr2d[1]

dbush
  • 205,898
  • 23
  • 218
  • 273