Assume the following C variable declaration: int *A[10], B[10][10]; Among the…
2018
Assume the following C variable declaration:
int *A[10], B[10][10];
Among the following expressions, which will not give compile time errors if used as left-hand side of assignment statements in a C program?
I. A[2] II. A[2][3] III. B[1] IV. B[2][3]
- A.
I, II and IV only
- B.
II, III and IV only
- C.
II and IV only
- D.
IV only
Attempted by 68 students.
Show answer & explanation
Correct answer: A
Expression I: A — Valid LHS
Ais a collections of 10 pointer variables.Atargets the third pointer variable inside that array. Because it is a normal pointer variable, its value (the memory address it holds) can be updated at any time.
Expression II: A — Valid LHS
Because
Ais anint*pointer, attaching a second subscript `` instructs the compiler to perform pointer arithmetic. It travels to the memory address stored inA, moves 3 integer steps forward, and looks at that specific integer. Since it resolves to a regularintvariable in memory, you can overwrite it.
Expression III: B — Invalid LHS (Triggers Compile Error)
Feedback:
Bis a 2D array, which means it is an array of arrays.Breferences the entire second row (an array of 10 integers). In C, an array name or row identifier decays into a constant pointer to its first element. You cannot assign anything to a constant pointer, nor can you overwrite an entire array block at once using an assignment operator.
Expression IV: B — Valid LHS
While you cannot assign a value to an entire row (like
B), you absolutely can assign a value to an individual element inside a row.Bpinpoints the specific single integer at row 2, column 3. It is a modifiable l-value.