Assume the following C variable declaration C int *A [10], B[10][10]; Of the…
2003
Assume the following C variable declaration
C
int *A [10], B[10][10];
Of the following expressions :
A[2]
A[2][3]
B[1]
B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
- A.
I, II, and IV only
- B.
II, III, and IV only
- C.
II and IV only
- D.
IV only
Attempted by 115 students.
Show answer & explanation
Correct answer: A
Given the declarations: int *A[10], B[10][10];
A[2]: A is an array of pointers to int, so A[2] has type int*. This is an assignable lvalue (you can assign a pointer value to it).
A[2][3]: This is equivalent to *(A[2] + 3). It names an int element (after dereferencing), so it is an assignable int lvalue.
B[1]: B is an array of arrays (type int[10][10]), so B[1] has type int[10], an array. Entire arrays are not assignable in C, so B[1] cannot be the left-hand side of an assignment.
B[2][3]: This refers to an int element of the two-dimensional array and is an assignable int lvalue.
Conclusion: The expressions that can be used as left-hand sides are A[2], A[2][3], and B[2][3]. B[1] cannot be used because it is an array and arrays cannot be assigned to.