Consider the following $\text{ANSI C}$ program.
#include <stdio.h> int main() { int arr[4][5]; int i, j; for (i=0; i<4; i++) { for (j=0; j<5; j++) { arr[i][j] = 10 * i + j; } } printf(“%d”, *(arr[1]+9)); return 0; }
What is the output of the above program?
Row Major Order is used in C Language, therefore the following mapping between logical 2D array to physical 1D memory happens in a C program.
With this idea in mind, We will also note that
arr[1] is a pointer to the first element of the 1st row.
To get to arr[1] + 9, we need to follow the row-major order.
$arr[1]+9$
$arr[1]$ is a pointer to 1D array of $5$ integers and so the above expression involves pointer arithmetic.
For a pointer value $p$ and integer value $d,$
So,
arr[1]+9 = *(arr+1) + 9 //arr is again a pointer but to the 2D array arr[4][5] arr+1 = arr + sizeof(*arr) = arr + 5 * sizeof (int) *(arr+1)+9 = arr + 5 * sizeof(int) + sizeof(**arr) * 9 //* operator in *(arr+1) just changes the type of the pointer here = arr + 5 * sizeof(int) + 9 * sizeof(int) = arr + 14 * sizeof(int)
Now, C language follows row major ordering for arrays which means that when a multi dimensional array gets linearized in memory the lower dimensions get arranged contiguously. For the 2D array $arr[4][5]$ it’ll be
$5$ elements of $arr[0][5]$ followed by $5$ elements of $arr[1][5]$ followed by $5$ elements of $arr[2][5]$ and so on.
So, the $14^{th}$ element will be at row number $\lfloor 14/5 \rfloor = 2$ and column number $14\%5 =4,$ which is $arr[2][4] = 10\times 2+4 = 24.$
More Read: https://gatecse.in/chapter-3-pointers/
You can add a for loop like below to go through all the element which are being parsed like
printf(“%d”, *(arr[1]+9));
Use pythontutor for understanding code: pythontutor.com/c.html#mode=edit
Here’s the code :
#include <stdio.h>
int main() { int arr[4] [5] ;
int i, j, k;
for (i=0; i<4; i++) { for (j=0; j<5; j++){ arr[i][j] = 10*i + j; } } for(k=0; k<12; k++){ printf("%d ", *(arr[1]+k)); } printf ("%d", *(arr[1] + 9) );
return 0; }
We know that a[4][5] is a 2D matrix which has 4 rows and 5 columns,
Logical representation:
[0][0] [0][1] [0][2] [0][3] [0][4]
[1][0] [1][1] [1][2] [1][3] [1][4]
[2][0] [2][1] [2][2] [2][3] [2][4]
[3][0] [3][1] [3][2] [3][3] [3][4]
In memory:
(All multi-dimensional arrays in C get linearized in memory row by row as C standard mandated row major ordering for arrays)
[0][0] [0][1] [0][2] [0][3] [0][4] [1][0] [1][1] [1][2] [1][3] [1][4] [2][0] [2][1] [2][2] [2][3] [2][4] [3][0] [3][1] [3][2] [3][3] [3][4]
Let’s understand what *(a[1]+9) means:
It’s nothing but value at a[1][9] i.e value at 1st row 9th col. Now, we are not able to find this index that’ll be our next question. Since C doesn’t check for array index range it searches in row-major order so, we need to start from a[1][0] and move to 9th place from here in memory ([1][1] will be your 1st jump).
So, 9th place is [2][4] i.e [1][9] = [2][4]
Hence, 10 * 2 + 4 = 24 (Option C)
Yes, a[1][0] will be your start position/index and in the above comment what I meant is a[1][1] will be your 1st step/jump from a[1][0]. Hope this clarifies your doubt.
so basically 15th element, in above answers, it was confusing that which element no. everybody wrote 14th element, which will be 2,3 or 23. but now I got it.
64.3k questions
77.9k answers
244k comments
80.0k users