Difficulty: Easy
Correct Answer: Error: in va_start(ptr, s);
Explanation:
Introduction / Context:
This question probes two classic varargs mistakes: (1) va_start
must be given the last named parameter in the function's parameter list, and (2) accidental shadowing of parameters with local variables. Both lead to broken argument retrieval and undefined behavior.
Given Data / Assumptions:
void display(char *s, int num1, int num2, ...)
.char s;
shadows the pointer parameter char *s
.va_start(ptr, s)
is used incorrectly.
Concept / Approach:
The second argument of va_start
must be exactly the identifier of the last named parameter before the ellipsis. In this prototype, that is num2
. Using s
(and worse, a local variable that shadows the parameter) violates the contract; the runtime will compute the wrong starting address for the variable arguments. Shadowing also confuses readers and compilers, but the key semantic error here is the bad va_start
anchor.
Step-by-Step Solution:
Parameter list ends with int num2
before ...
.Correct anchor: va_start(ptr, num2);
Remove the conflicting char s;
or rename it.Retrieve floating arguments as double
due to default promotions.
Verification / Alternative check:
Fixing to va_start(ptr, num2)
and removing the shadowed s
produces stable output. Tools like sanitizers can help detect UB caused by wrong va_start
.
Why Other Options Are Wrong:
Invalid arguments and too many parameters are generic and do not pinpoint the varargs rule being broken. No error is clearly incorrect.
Common Pitfalls:
Anchoring va_start
to the wrong parameter, shadowing names, and forgetting default promotions.
Final Answer:
Error: in va_start(ptr, s);
Discussion & Comments