retagged by
29,904 views
38 votes
38 votes

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?

  1. $14$
  2. $20$
  3. $24$
  4. $30$
retagged by

11 Answers

Best answer
16 votes
16 votes

$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,$

  • $p + d \implies p + sizeof(*p) + 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/

selected by
20 votes
20 votes

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)

10 votes
10 votes
*(arr[1]+9) = arr[1][9]

but arr declared as int arr[4][5], due to C doesn’t check array index range and follow row major order

arr[1][5] = arr[2][0]

arr[1][6] = arr[2][1]

arr[1][7] = arr[2][2]

arr[1][8] = arr[2][3]

arr[1][9] = arr[2][4]

 

arr[2][4] = 10*i + j = 10*2+4 = 24

Option C is the answer.
Answer:

Related questions

11 votes
11 votes
4 answers
1
6 votes
6 votes
1 answer
3