Consider the following ANSI C program. #include < stdio.h > int main( ) { int…

2021

Consider the following 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. A.

    14

  2. B.

    20

  3. C.

    24

  4. D.

    30

Attempted by 102 students.

Show answer & explanation

Correct answer: C

Key idea: a two-dimensional array is stored in row-major order and pointer arithmetic treats it as a contiguous sequence of elements.

  • Each element is arr[i][j] = 10*i + j.

  • arr[1] points to arr[1][0], whose linear index (counting from arr[0][0]) is 1*5 + 0 = 5.

  • Adding 9 moves to linear index 5 + 9 = 14.

  • Convert linear index 14 back to row and column for 5 columns: row = 14 / 5 = 2, column = 14 % 5 = 4. So the accessed element is arr[2][4].

  • Evaluate arr[2][4] = 10*2 + 4 = 24.

Therefore, the program prints 24.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir