If P is a two-dimensional array having 10 rows and 20 columns, then which of…
2017
If P is a two-dimensional array having 10 rows and 20 columns, then which of the following cannot be used to access the element in row 2 and column 5?
- A.
P[2][5]
- B.
*(*(P + 2) + 5)
- C.
*(P[2] + 5)
- D.
*(P + 2 + 5)
Attempted by 547 students.
Show answer & explanation
Correct answer: D
In C, a 2D array element $P[i][j]$ is equivalent to *(*(P + i) + j) or *(P[i] + j). To access row 2 and column 5, the valid expressions are P[2][5], *(*(P + 2) + 5), and *(P[2] + 5).
Expression *(P + 2 + 5) simplifies to *(P + 7), which incorrectly accesses the start of row 7 instead of the element at P[2][5].
Correct Answer: *(P + 2 + 5) (Option D cannot be used).