Definition | #include <stdarg.h> <type> va_arg(va_list arg_p, <type>); Together with the The Before | |
Parameters | va_list arg_p Pointer to the argument list initialized with <type> Type name matching the type of the current argument. All C data types are valid for which a pointer to an object of type type is defined by simply appending * to type. Array and function types, for example, are invalid. | |
Return val. | Value of the argument | |
The first call after | ||
Undefined | The behavior is undefined if there is no next argument or <type> does not match the current argument. | |
Notes | Compatibility of argument types is supported by the C runtime system to the extent that similar types are stored in the same way in the parameter list: The macro | |
Example | The f1 function fills an array with a list of arguments which are of the type pointer to string. #include <stdarg.h>
#include <stdio.h>
#define MAXARGS 20
extern int f2(int i, char *a[]);
void f1(int n_ptrs, ...)
{
va_list ap;
char *array[MAXARGS];
int ptr_no = 0;
if (n_ptrs > MAXARGS)
n_ptrs = MAXARGS;
va_start(ap, n_ptrs);
while (ptr_no < n_ptrs)
array[ptr_no++] = va_arg(ap, char *);
va_end(ap);
f2(n_ptrs, array);
return 0;
}
| |
See also | va_start, va_end | |