Skip to content
Official PYQ · 2020

GATE CS 2020 Q22 — Programming in C

Explain with AI

Consider the following C program.

#include <stdio.h>
int main ()  {
    int  a[4] [5] = {{1, 2, 3, 4, 5},
                    {6, 7,8, 9, 10},
                    {11, 12, 13, 14, 15},
                    {16, 17,18, 19, 20}};
    printf(“%d\n”, *(*(a+**a+2)+3));
    return(0);
}

The output of the program is _______.

View worked solution

Answer: 19

To find the output of the program, we need to evaluate the expression *(*(a + **a + 2) + 3) step-by-step.

Step 1: Understand the Array Structure

The 2D array a is declared as int a[4][5]. It consists of 4 rows and 5 columns:

  • Row 0 (a[0]): {1, 2, 3, 4, 5}
  • Row 1 (a[1]): {6, 7, 8, 9, 10}
  • Row 2 (a[2]): {11, 12, 13, 14, 15}
  • Row 3 (a[3]): {16, 17, 18, 19, 20}

Step 2: Evaluate **a

  • a is a pointer to the first row (type int (*)[5]).
  • *a is a pointer to the first element of the first row (type int *), which is equivalent to &a[0][0].
  • **a is the value stored at &a[0][0], which is a[0][0] = 1.

Step 3: Substitute **a into the Expression

Substituting **a = 1 into the original expression:

∗(∗(a+∗∗a+2)+3)  ⟹  ∗(∗(a+1+2)+3)  ⟹  ∗(∗(a+3)+3)*(*(a + **a + 2) + 3) \implies *(*(a + 1 + 2) + 3) \implies *(*(a + 3) + 3)

Step 4: Evaluate *(*(a + 3) + 3)

  • a + 3 points to the 4th row of the 2D array (index 3).
  • *(a + 3) is equivalent to a[3], which is a pointer to the first element of the 4th row, i.e., &a[3][0].
  • *(a + 3) + 3 is equivalent to &a[3][0] + 3, which points to a[3][3].
  • Therefore, *(*(a + 3) + 3) is equivalent to a[3][3].

Step 5: Find the Value of a[3][3]

Looking at the 4th row (index 3):

  • a[3][0] = 16
  • a[3][1] = 17
  • a[3][2] = 18
  • a[3][3] = 19

Thus, the output of the program is 19.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions