Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion with Two Dimensional Array

Please consider the following 2-D Array:

int array[2][2] = {
                    {1,2},
                    {3,4}
                  };

As per my understanding: - 'array' represents the base address of the 2-D array (which is the same as address of the first element of the array, i.e array[0][0]).

  • The actual arrangement of a 2-D Array in memory is like a large 1-D Array only.

  • Now, I know that base address = array. Hence, I should be able to reach the Memory Block containing the element: array[0][0].

  • If I forget about the 2-D array thing & try to treat this array as a simple 1-D array: array[0] = *(array+0) gives the base address of the first array & NOT the element array[0][0]. Why?

  • A 2-D array does not store any memory address (like an Array of Pointers).

  • If I know the base address, I must be able to access this memory as a linear 1- Dimensional Array.

Please help me clarify this doubt.

Thanks.

like image 328
Sandeep Singh Avatar asked Jul 29 '26 13:07

Sandeep Singh


2 Answers

array[0] is a one-dimensional array. Its address is the same as the address of array and the same as the address of array[0][0]:

assert((void*)&array == (void*)&(array[0]));
assert((void*)&array == (void*)&(array[0][0]));

Since array[0] is an array, you can't assign it to a variable, nor pass it to a function (if you try that, you'll be passing a pointer to the first element instead). You can observe that it's an array by looking at (array[0])[0] and (array[0])[1] (the parentheses are redundant).

printf("%d %d\n", (array[0])[0], (array[0])[1]);

You can observe that its size is the size of 2 int objects.

printf("%z %z %z\n", sizeof(array), sizeof(array[0]), sizeof(array[0][0]));

Here's a diagram that represents the memory layout:

+-------------+-------------+-------------+-------------+
|      1      |      2      |      3      |      4      |
+-------------+-------------+-------------+-------------+
 `array[0][0]' `array[0][1]' `array[1][0]' `array[1][1]'
`---------array[0]---------' `---------array[1]---------'
`-------------------------array-------------------------'
like image 52
Gilles 'SO- stop being evil' Avatar answered Jul 31 '26 03:07

Gilles 'SO- stop being evil'


"Thou shalt not fear poynter arythmethyc"...

int array[2][2] = { { 1, 2}, { 3, 4 } };
int *ptr = (int *)&array[0][0];
int i;
for (i = 0; i < 4; i++) {
    printf("%d\n", ptr[i]);
}

Why does this work? The C standard specifies that multidimensional arrays are contigous in memory. That means, how your 2D array is arranged is, with regards to the order of its elements, is something like

array[0][0]
array[0][1]
array[1][0]
array[1][1]

Of course, if you take the address of the array as a pointer-to-int (int *, let's name it ptr), then the addresses of the items are as follows:

ptr + 0 = &array[0][0]
ptr + 1 = &array[0][1]
ptr + 2 = &array[1][0]
ptr + 3 = &array[1][1]

And that's why it finally works.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!