Similar questions 5 more →
Pointers & Memory
![Question 22, GATE 2020: Find the output of this C program:
#include <stdio.h>
int main(void) {
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;
}](../../assets/questions/CS-2020-Q32-1.webp)
Answer & solution
Official answer: 19
Step through it (6 steps)
Worked explanation
Track pointer types: in this expression a decays to a pointer to an array of five ints, so adding 1 advances by a whole row. *a denotes row zero, and a is its first integer, 1. Therefore a+a+2 is a+3. Dereferencing gives row three, which in the addition +3 decays to a pointer to its first int. Adding 3 selects its fourth entry and the outer dereference reads 19. Written with subscripts the expression is a[3][3], not a[0][6] or a linear six-element offset. Both row and column indices are valid.
Derivation and answer
Here a=a[0][0]=1. Thus a+a+2=a+3 points to the fourth row. Dereferencing that row and adding 3 selects a[3][3]=19. All pointer arithmetic stays within the relevant row/outer array, so the printed expression has the defined result 19.