In C (pointer decay with multidimensional arrays), what will this print if the array 'a' begins at address 1002? #include<stdio.h> int main() { int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2}, {2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} }; printf("%u, %u, %u, %d ", a, *a, **a, ***a); return 0; }

Difficulty: Medium

Correct Answer: 1002, 1002, 1002, 1

Explanation:


Introduction / Context:
This question checks your understanding of how multidimensional arrays decay to pointers in C, what a, *a, and **a mean for a 3-D array, and how dereferencing down to **a yields the first stored integer. The addresses are based on a provided base location (1002).


Given Data / Assumptions:

  • The array has type int a[2][3][4] and begins at address 1002.
  • printf uses %u for printing pointer values (strictly, standard requires %p, but we follow the problem’s convention).
  • Array-to-pointer decay applies when array expressions appear in function calls.


Concept / Approach:
For int a[2][3][4], the identifiers behave as follows: a → pointer to first element, type int ()[3][4], value &a[0]a → the first element, type int [3][4]; when passed to printf, it decays to &a[0][0] (type int ()[4])*a → the first row inside that, type int [4]; it decays to &a[0][0][0] (type int)***a → the first scalar int value stored, which is 1 from the initializerAll three pointers point to the same starting address of the entire object’s first scalar element.


Step-by-Step Solution:
a → &a[0] → address 1002*a → a[0] → decays to &a[0][0] → address 1002**a → a[0][0] → decays to &a[0][0][0] → address 1002***a → a[0][0][0] → integer value 1


Verification / Alternative check:
Manually expand indices: a == &a[0]*a == a[0] and &a[0][0] is where the first 4-int row begins**a == a[0][0] and &a[0][0][0] is the first scalar


Why Other Options Are Wrong:
(a) and (b) assume incorrect scaling to different addresses; all three decayed pointers start at the same base. (d) No compile-time error for this code. (e) The problem pins the base address; under those assumptions, the triplet is not variable.


Common Pitfalls:
Confusing types of a, *a, and **a; assuming each must advance to a different address; or thinking ***a is a pointer rather than a scalar.


Final Answer:
1002, 1002, 1002, 1

More Questions from Pointers

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion