Double dereference on incremented argv pointer: which character prints first?\n\n/* myprog.c */\n#include <stdio.h>\n\nint main(int argc, char *argv)\n{\n / Invoked as: myprog one two three */\n printf("%c\n", ++argv);\n return 0;\n}\n

Difficulty: Easy

Correct Answer: o

Explanation:


Introduction / Context:
The snippet highlights pointer-to-pointer manipulation with argv. It advances the argv pointer itself and then dereferences twice to reach the first character of the next argument string.


Given Data / Assumptions:

  • argv initially points to argv[0] ("myprog").
  • After ++argv, it points to argv[1] ("one").
  • ++argv dereferences to the first char of argv[1].


Concept / Approach:
++argv increments the pointer-to-pointer so that argv becomes the next char, which is the C-string for the first argument. A second dereference (**) then gives the first character of that string.


Step-by-Step Solution:

Start: argv → &argv[0].++argv → argv now points at &argv[1].*argv → argv[1] → "one".**argv → first char of "one" → 'o'.


Verification / Alternative check:
Printing %s with *++argv would display "one"; switching to %c and ** prints its first character.


Why Other Options Are Wrong:

  • myprog one two three / myprog one / two: These would require printing strings, not a single character.


Common Pitfalls:
Confusing incrementing argv versus incrementing an element like argv[1]; mixing up * and ** dereference levels.


Final Answer:
o

More Questions from Command Line Arguments

Discussion & Comments

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