Double dereference on incremented argv pointer: which character prints first?
/* myprog.c */
#include
int main(int argc, char *argv)
{
/ Invoked as: myprog one two three */
printf("%c
", ++argv);
return 0;
}
-
Amyprog one two three
-
Bmyprog one
-
Co
-
Dtwo
Answer
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