GATE / CS ARCHIVE

GATE CSE 2021 SET 2 / QUESTION 10

GATE CSE 2021 Set 2 Q10

Programming in C · Pointers & Memory · Full GATE CSE 2021 Set 2 paper

GATE 2021 Set 2, Q10 1 mark · MCQ
Report issue

Pointers & Memory

Question 10, GATE 2021: ANSI C declares int arr[4][5] and initializes arr[i][j]=10*i+j for 0≤i<4,0≤j<5. It prints *(arr[1]+9) with %d. Offered outputs: A 14; B 20; C 24; D 30. Explain the intended result and the language-standard issue.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Our answer: C under intended flattening; undefined in strict C

The intended contiguous-address calculation begins at row1, column 0, linear index 5; advancing 9 integers reaches index 14=row2, column 4, whose initialized value is 24, C. However arr[1] decays to a pointer into the five-element row subarray. Adding9 goes beyond that array and its one-past position, so the expression has undefined behavior under C pointer-arithmetic rules. Contiguous storage of the outer array does not legalize this cross-row pointer arithmetic. The official C is retained as the intended exam answer, not a portable guarantee.

The intended memory layout has rows 0–4,10–14,20–24,30–34. The desired element is arr[2][4], which is a portable way to express it. Do not run this original expression and treat one observed compiler output as a proof of defined behavior. The official option describes the conventional exam address calculation only.

Original paper, page 60 · Answer key