GATE / CS ARCHIVE

GATE CSE 2003 / QUESTION 2

Assignable array and pointer expressions

Programming in C · Pointers & Memory · Full GATE CSE 2003 paper

GATE 2003, Q2 1 mark · MCQ
Report issue

Pointers & Memory

Question 2, GATE 2003: Given int *A[10], B[10][10]; consider I. A[2], II. A[2][3], III. B[1], IV. B[2][3]. Which expressions can be left-hand sides of assignments without compile-time errors, assuming type-compatible right-hand sides? A. I, II and IV only; B. II, III and IV only; C. II and IV only; D. IV only.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Resolve the declarations before considering assignment: A[i] has type int*, while B[i] has array type int[10]. Applying another subscript dereferences an element and yields int in either case. Arrays are not modifiable lvalues, but pointer objects and int objects are. No actual dereference safety can be inferred from these declarations alone.

2. A is an array of 10 pointers to int. A[2] is an assignable pointer object, and A[2][3] is an assignable int lvalue if the pointer identifies suitable storage. B[2][3] is likewise an int lvalue. B[1] is an array row and cannot be assigned as a whole in C. Thus I,II,IV, option A, with type-compatible right-hand sides; compile-time validity does not establish safe runtime initialization.

Original paper, page 1