C varargs across functions: identify the incorrect usage when forwarding varargs and misusing va_start.
#include
#include
void display(char *s, ...);
void show(char *t, ...);
int main()
{
display("Hello", 4, 12, 13, 14, 44);
return 0;
}
void display(char s, ...)
{
show(s, ...); / attempt to forward ellipsis directly */
}
void show(char t, ...)
{
int a;
va_list ptr;
va_start(ptr, s); / wrong anchor, should be 't' */
a = va_arg(ptr, int);
printf("%f", a);
}
-
AError: invalid function display() call
-
BError: invalid function show() call
-
CNo error
-
DError: Rvalue required for t
-
ENone of the above
Answer
Correct Answer: Error: invalid function show() call
Explanation
Introduction / Context:Forwarding variable arguments between functions requires care. You cannot “pass” ... directly; instead you must rebuild the call with explicit arguments or use va_list/va_copy. Moreover, va_start must reference the last named parameter in the definition's parameter list.
Given Data / Assumptions:
displayattemptsshow(s, ...), which is not valid C syntax for forwarding varargs.showcallsva_start(ptr, s)even though its last named parameter ist, nots.printf("%f", a)prints anintwith a%fspecifier, which is also mismatched.
Concept / Approach:The error most centrally tested is in show: va_start must be invoked as va_start(ptr, t), because t is the last named parameter. Additionally, forwarding from display requires building a va_list inside display and then either consuming it there or using va_copy to pass to show. Writing show(s, ...) is invalid.
Step-by-Step Solution:Fix display to accept va_list forwarding: e.g., have show accept va_list or reconstruct arguments.In show, replace va_start(ptr, s) with va_start(ptr, t).Match printf format with type: use %d for int.
Verification / Alternative check:Correct patterns include: void vshow(char *t, va_list ap); and calling it from display with va_start/va_copy.
Why Other Options Are Wrong:Invalid function display() call is also problematic but the more concrete, immediate error flagged is within show (wrong va_start anchor and format mismatch). No error and other distractors ignore these standard violations.
Common Pitfalls:Trying to forward ... literally, using the wrong identifier in va_start, and mismatching printf formats.
Final Answer:Error: invalid function show() call