Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

va_arg - Process variable argument list

&pagelevel(4)&pagelevel


Definition

#include <stdarg.h>

<type> va_arg(va_list arg_p, <type>);

Together with the va_start and va_end macros, the va_arg macro is used to process a list of arguments which may vary in number and type from function call to function call. A variable argument list is indicated in the formal parameter list of the function definition by ", ...".

The va_arg macro returns the data type and value of the next argument in a variable argument list, beginning with the first argument. Technically speaking, the macro expands into an expression of the data type and value of the argument.

Before va_arg is called for the first time, the variable argument list to which arg_p points must be initialized with va_start. Each time va_arg is called, arg_p changes so that the value of the next argument is made available.

Parameters 

va_list arg_p

Pointer to the argument list initialized with va_start before va_arg is called for the first time.

<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 va_start is called returns the value of the first argument.
This argument comes after the last “named” argument parmN in the formal parameter list (cf. va_start).
Subsequent calls return the remaining argument values in succession.

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:
All unsigned types (including char) are represented as unsigned int (right-justified in a word).
All other integer types are represented as int (right-justified in a word).
float is represented as double (right-justified in a doubleword).

The macro va_end must be called before the return from a function whose argument list has been processed with va_arg.

Example

The f1 function fills an array with a list of arguments which are of the type pointer to string.
No more than MAXARGS arguments are to be processed. The number of pointer arguments is defined as the first argument for f1. The filled array is than passed to function f2.

#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