Here are descriptions of the macros used to retrieve variable arguments. These macros are defined in the header file stdarg.h.
This macro initializes the argument pointer variable ap to point to the first of the optional arguments of the current function; last-required must be the last required argument to the function.
See Old Varargs, for an alternate definition of
va_start
found in the header file varargs.h.
The
va_arg
macro returns the value of the next optional argument, and modifies the value of ap to point to the subsequent argument. Thus, successive uses ofva_arg
return successive optional arguments.The type of the value returned by
va_arg
is type as specified in the call. type must be a self-promoting type (notchar
orshort int
orfloat
) that matches the type of the actual argument.
This ends the use of ap. After a
va_end
call, furtherva_arg
calls with the same ap may not work. You should invokeva_end
before returning from the function in whichva_start
was invoked with the same ap argument.In the GNU C library,
va_end
does nothing, and you need not ever use it except for reasons of portability.
Sometimes it is necessary to parse the list of parameters more than once
or one wants to remember a certain position in the parameter list. To
do this, one will have to make a copy of the current value of the
argument. But va_list
is an opaque type and one cannot necessarily
assign the value of one variable of type va_list
to another variable
of the same type.
The
__va_copy
macro allows copying of objects of typeva_list
even if this is not an integral type. The argument pointer in dest is initialized to point to the same argument as the pointer in src.This macro is a GNU extension but it will hopefully also be available in the next update of the ISO C standard.
If you want to use __va_copy
you should always be prepared for the
possibility that this macro will not be available. On architectures where a
simple assignment is invalid, hopefully __va_copy
will be available,
so one should always write something like this:
{ va_list ap, save; ... #ifdef __va_copy __va_copy (save, ap); #else save = ap; #endif ... }