Difficulty: Easy
Correct Answer: JACK
Explanation:
Introduction / Context:
This question demonstrates how a void
can point to different types at different times, provided you cast back to the appropriate pointer type before dereferencing. The format specifiers must also match the types of the dereferenced values.
Given Data / Assumptions:
ch = 74
which corresponds to the ASCII character 'J'.j = 65
which corresponds to the ASCII character 'A'.cp = "JACK"
is a string literal.
Concept / Approach:void*
is a generic pointer; it must be cast to the correct type before dereference. The first print casts to char*
; the second casts to int*
and prints the character represented by the integer value; the third prints a substring of the string literal by advancing two characters.
Step-by-Step Solution:
vp = &ch; print (char)vp → 'J'.vp = &j; print (int)vp as %c → 65 → 'A'.vp = cp; print (char*)vp + 2 → substring of "JACK" starting at index 2 → "CK".Concatenate outputs: "J" + "A" + "CK" = "JACK".
Verification / Alternative check:
Manually evaluate ASCII: 74 is 'J', 65 is 'A'. Substring of "JACK" from index 2 is "CK".
Why Other Options Are Wrong:
"JCK" omits the 'A'. "J65K" prints the integer value as digits, not a character; the code uses %c so the character is printed. "JAK" drops one character from the final substring.
Common Pitfalls:
Casting to the wrong type before dereference or mismatching format specifiers, which would cause undefined behavior or garbage output.
Final Answer:
JACK
Discussion & Comments