Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why *(arr+1) brings the same value as arr+1 in multi-dimensional arrays?

O haver a doubt about multi-dimensional arrays and pointers notation, as bellow.

arr[2][4] = {{1,2,3,4}, {5,6,7,8}};
printf("%d\n", (arr+1));
printf("%d\n", *(arr+1));

Why in the both printf(), the result printed in the screen is the same? Both bring the address of the second array inside of the arr.
The first one I understand because arr is a memory address and so, adding 1 to it adds (the size of the inner array * size of integer).
But the second one I am confused because if I put * before arr+1 it shoudn’t bring the value inside the address of arr+1 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

(arr) points to the first memory address of the 2 * 4 array, i.e., arr[0][0].
(arr+1) points to the 2nd 1-D array comprising arr, i.e., it points to the array arr[1].

Now, *(arr+1) is basically the address of the first element of the 2nd 1-D array comprising arr, i.e., it points to the element arr[1][0].

If you know pointers well, arr[1] is the same address as arr[1][0].

You can confirm that by checking **(arr+1), which will output 5 in your case.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading