Dereference of incremented argument pointer: identify the printed character from argv[2].\n\n/* sample.c /\n#include <stdio.h>\n\nint main(int argc, char argv[])\n{\n / Invoked as: sample friday tuesday sunday /\n printf("%c", ++argv[2]);\n return 0;\n}\n

Difficulty: Medium

Correct Answer: u

Explanation:


Introduction / Context:
The expression ++argv[2] combines array indexing, prefix increment, and dereferencing on a pointer to char. Understanding operator precedence and the effect of incrementing a char is crucial.


Given Data / Assumptions:

  • argv[2] points to the string "tuesday".
  • ++argv[2] advances the pointer to the next character in the same string.
  • then dereferences that advanced pointer to yield a single character.


Concept / Approach:
[] binds more tightly than ++, so we first get argv[2] (a char), then apply prefix ++ to move within the string, and finally use * to read the character now pointed to.


Step-by-Step Solution:

argv[2] initially points at 't' in "tuesday".++argv[2] moves to the next character, which is 'u'.++argv[2] yields 'u'.


Verification / Alternative check:
Assign char *p = argv[2]; ++p; putchar(*p); to observe the same result without compact operators.


Why Other Options Are Wrong:

  • s / r / f: Other characters from different positions or different strings; the code specifically targets the second character of argv[2].


Common Pitfalls:
Misinterpreting ++argv[2] as ++argv then [2]; forgetting pre-increment advances before dereference.


Final Answer:
u

More Questions from Command Line Arguments

Discussion & Comments

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