In C (pointers to pointers), given 4-byte int, what does this print? #include<stdio.h> int main() { int ***r, **q, *p, i = 8; p = &i; q = &p; r = &q; printf("%d, %d, %d ", *p, **q, ***r); return 0; }

Difficulty: Easy

Correct Answer: 8, 8, 8

Explanation:


Introduction / Context:
The exercise ensures you can trace multiple levels of indirection. The pointers p, q, and r successively point to the location of i, the location of p, and the location of q. Dereferencing the correct number of times should always yield the integer value stored in i.


Given Data / Assumptions:

  • i = 8
  • p = &i → *p reads i
  • q = &p → **q reads *(&i) → i
  • r = &q → ***r reads **(&p) → i


Concept / Approach:
At each level, dereference once to move toward the underlying object. The star count must match the pointer depth: one star for p, two for q, three for r. All evaluate to the same stored scalar.


Step-by-Step Solution:
*p = i = 8**q = ((&i)) = *(&i) = i = 8**r = **((&p)) = **(&p) = *p = i = 8


Verification / Alternative check:
Temporarily print addresses to confirm the chain: &i == p&p == q&q == r and match the dereference counts.


Why Other Options Are Wrong:
(b)–(d) list pretend addresses instead of values. The code prints integers obtained by dereferencing, not addresses.


Common Pitfalls:
Confusing the number of dereferences; printing pointers rather than values; assuming pointer size affects the printed values here.


Final Answer:
8, 8, 8

More Questions from Pointers

Discussion & Comments

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