Difficulty: Medium
Correct Answer: 1006, 2, 2
Explanation:
Introduction / Context:
Understanding how C lays out multidimensional arrays (row-major order) and how pointers relate to array elements is essential for correct indexing and pointer arithmetic. This example asks you to compute both an address and two dereferenced values when given a base address and sizeof(int).
Given Data / Assumptions:
Concept / Approach:
a[0] has type int and points to a[0][0]. Adding 1 to a[0] advances the pointer by one int, i.e., +4 bytes. Dereferencing (a[0]+1) reads a[0][1]. The expression ((a+0)+1) is the same as a[0][1].
Step-by-Step Solution:
Verification / Alternative check:
Index equivalence: a[i][j] equals ((a+i)+j). With i=0, j=1 we get the same element both ways.
Why Other Options Are Wrong:
Common Pitfalls:
Adding 1 to a pointer moves it by sizeof(element), not 1 byte; confusing addresses with values; forgetting row-major ordering.
Final Answer:
1006, 2, 2.
Discussion & Comments