macro
<cstdarg>
va_start
void va_start (va_list ap, paramN);
Initialize a variable argument list
Initializes ap to retrieve the additional arguments after parameter paramN.
A function that invokes va_start, shall also invoke va_end before it returns.
Parameters
- ap
- Uninitialized object of type va_list.
After the call, it carries the information needed to retrieve the additional arguments using va_arg.
If ap has already been passed as first argument to a previous call to va_start or va_copy, it shall be passed to va_end before calling this function.
- paramN
- Name of the last named parameter in the function definition. The arguments extracted by subsequent calls to va_arg are those after paramN.
The parameter shall not be a parameter declared with register
storage class, with function or array type, or with a type that is not compatible with the type that results after application of the default argument promotions.
The parameter shall not be of a reference type, or of a type that is not compatible with the type that results when passing an argument for which there is no parameter.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
/* va_start example */
#include <stdio.h> /* printf */
#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
void PrintFloats (int n, ...)
{
int i;
double val;
printf ("Printing floats:");
va_list vl;
va_start(vl,n);
for (i=0;i<n;i++)
{
val=va_arg(vl,double);
printf (" [%.2f]",val);
}
va_end(vl);
printf ("\n");
}
int main ()
{
PrintFloats (3,3.14159,2.71828,1.41421);
return 0;
}
|
The function PrintFloats takes the number of additional arguments as first argument (n), which are then sequentially read using the cstdarg macro and printed out with a specific format.
Output:
Printing floats: [3.14] [2.72] [1.41]
|
See also
- va_arg
- Retrieve next argument (macro)
- va_end
- End using variable argument list (macro)
- va_list
- Type to hold information about variable arguments (type)
- vsprintf
- Write formatted data from variable argument list to string (function)