• retagged by
49,535 views
80 80 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$

12 Answers

Best answer
41 41 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
52 52 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)

16 16 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.
11 11 votes

After the for loop, the arr[4][5] will look like below table

arr[4][5]
0 1 2 3 4
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34


Now *(arr[1]+9) can be translated as: *(*(arr+1)+9)
Now step by step resolving it:

arr[4][5]
0 1 2 3 4
10 *(arr[1]+0) 11 *(arr[1]+1) 12 *(arr[1]+2) 13 *(arr[1]+3) 14 *(arr[1]+4)
20 *(arr[1]+5) 21 *(arr[1]+6) 22 *(arr[1]+7) 23 *(arr[1]+8) 24 *(arr[1]+9)
30 31 32 33 34


Hence C. 24 is the answer.

4 4 votes

If you the know the subtle difference between (a+1) and *(a+1) this can be solved within 30 seconds.

(a+1) → points to the entire 2nd row. It is a pointer to an integer array of size 5.

*(a+1) → points to the 1st element of the 2nd row. It is a pointer to an integer.

Now *(arr[1] + 9) can be interpreted as *(*(arr +1) + 9)

  → Which means we need to go to the 2nd row and access the 9th element from the starting of the 2nd Row which is finally arr[2][4] = 10 * 2 + 4 = 24

To understand please refer to the diagram below:

 

 

 

 

 

 

 

 

Solution : Option (C)

• edited by
Answer:
Position:
Show:

Related questions

24 24 votes
7 answers 7 answers
10.6k
10.6k views
Arjun asked Feb 18, 2021
10,572 views
Consider the following $\text{ANSI C}$ function:int SomeFunction (int x, int y) { if ((x==1) || (y==1)) return 1; if (x==y) return x; if (x y) return SomeFunction(x-y, y...
36 36 votes
3 answers 3 answers
16.9k
16.9k views
Arjun asked Feb 18, 2021
16,890 views
Consider the following $\text{ANSI C}$ program#include <stdio.h int foo(int x, int y, int q) { if ((x<=0) && (y<=0)) return q; if (x<=0) return foo(x, y-q, q); if (y<=0)...
55 55 votes
5 answers 5 answers
22.1k
22.1k views
Arjun asked Feb 18, 2021
22,114 views
Consider the following $\text{ANSI C}$ program:#include <stdio.h #include <stdlib.h struct Node{ int value; struct Node *next;}; int main( ) { struct Node *boxE, *head, *...