Difficulty: Easy
Correct Answer: Error: ptr must be type of va_list
Explanation:
Introduction / Context:
Using variable arguments in C requires a dedicated opaque type va_list
. You cannot substitute other pointer types for va_list
. The macros va_start
, va_arg
, and va_end
are defined to operate only on objects of type va_list
.
Given Data / Assumptions:
ptr
is declared as float *
.va_start(ptr, n)
is invoked with ptr
.va_arg
is called using the wrong ptr
type.
Concept / Approach:va_list
is an implementation-defined type that stores the state necessary to iterate over unnamed arguments. Treating it as an ordinary pointer (e.g., float *
) is invalid and will not compile or will result in undefined behavior. The correct declaration is va_list ptr;
followed by va_start(ptr, n);
.
Step-by-Step Solution:
Replace float *ptr;
with va_list ptr;
Invoke va_start(ptr, n);
to initialize list traversal.Use va_arg(ptr, int)
to retrieve the first promoted integer.Finish with va_end(ptr);
Verification / Alternative check:
Compiling with a conforming compiler will error on applying va_start
to a non-va_list
object. Static analyzers flag this immediately.
Why Other Options Are Wrong:
Too many parameters is unrelated. Invalid access to list member is vague and not the fundamental issue. No error is incorrect because the type is wrong.
Common Pitfalls:
Assuming va_list
is just a pointer; it may be a struct or array type internally. Always use the standard macros and types.
Final Answer:
Error: ptr must be type of va_list
Discussion & Comments