GATE / CS ARCHIVE

GATE CSE 2002 / QUESTION 2.8

Address of an element of a C array

Programming in C · Arrays & Strings · Full GATE CSE 2002 paper

GATE 2002, Q2.8 2 marks · MCQ
Report issue

Arrays & Strings

Question 2.8, GATE 2002: C declares char a[100][100]. Memory is byte-addressable, and the array starts at address 0. What is the address of a[40][50]?
(A) 4040; (B) 4050; (C) 5040; (D) 5050.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step 1: Compute the linear index before the byte address.

Step 2: There are 40 complete rows before row40, each contributing 100 characters, and 50 earlier characters within that row. Therefore the zero-based linear index is 4000+50=4050. Since sizeof(char)=1, no further scale factor is needed. The5040 alternative reverses the row and column roles.

Conclusion: C lays out rows contiguously in row-major order. Each char occupies one byte, and each row has 100 elements. The offset of a[40][50] is 40×100+50=4050 bytes, giving address 4050 from the stipulated zero base, B.

Original paper, page 4