Dereference of incremented argument pointer: identify the printed character from argv[2].
/* sample.c /
#include
int main(int argc, char argv[])
{
/ Invoked as: sample friday tuesday sunday /
printf("%c", ++argv[2]);
return 0;
}
-
As
-
Bf
-
Cu
-
Dr
Answer
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