In C, address-of and dereference cancellation with a char pointer: what is printed? #include<stdio.h> int main() { char p; p = "hello"; printf("%s ", &&p); return 0; }

Difficulty: Easy

Correct Answer: hello

Explanation:


Introduction / Context:
This short puzzle shows that applying address-of (&) and dereference () operators in alternating fashion can cancel out, if they are applied in a type-consistent way. The format string %s expects a char* to a null-terminated string.


Given Data / Assumptions:

  • p points to the string literal "hello".
  • The expression is &&p (equivalent to p).
  • We print with printf("%s\n", ...).


Concept / Approach:
Reading right-to-left for unary operators: the innermost is &p (address of p), then (&p) which yields p, then &((&p)) which is &p again, and finally (&p) which returns p. So the entire expression is simply p, a char to the literal.


Step-by-Step Solution:
Evaluate &&p → pprintf receives p as a char*It prints the full null-terminated string: hello


Verification / Alternative check:
Replacing the expression with just p yields identical output.


Why Other Options Are Wrong:
(a), (c), and (d) suggest starting at later characters; nothing in the code increments the pointer. (e) There is no invalid access here; reading a string literal via %s is fine (modification would not be).


Common Pitfalls:
Assuming address-of and dereference operators always change the value; when paired correctly, they cancel out.


Final Answer:
hello

More Questions from Pointers

Discussion & Comments

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