This set of macros allows portable procedures that accept variable numbers of arguments of variable types to be written. Routines that have variable argument lists (such as printf)
but do not use stdarg are inherently non-portable, as different machines use different argument-passing conventions.
va_list is a type defined for the variable used to traverse the list.
The va_start() macro is invoked before any access to the unnamed arguments and initializes pvar for subsequent use by va_arg() and va_end(). The parameter parmN is the identifier of the rightmost parameter in the variable parameter list in the function definition (the one just before the , ...). If this parameter is declared with the register storage class or with a 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 behavior is undefined.
The parameter parmN is required under strict ANSI C compilation. In other compilation modes, parmN need not be supplied and the second parameter
to the va_start() macro can be left empty (for example, va_start(pvar, );). This allows for routines that contain no parameters before the ... in
the variable parameter list.
The va_arg() macro expands to an expression that has the type and value of the next argument in the call. The parameter pvar should have been previously initialized
by va_start(). Each invocation of va_arg() modifies pvar so that the values of successive arguments are returned in turn. The parameter type is the type name of the next argument to be returned. The type name must be specified in such a way so that the type of a pointer to an object that has the specified type can be obtained
simply by postfixing a * to type. If there is no actual next argument, or if type is not compatible with the type of the actual next argument (as
promoted according to the default argument promotions), the behavior is undefined.
The va_copy() macro saves the state represented by the va_list src in the va_list dest.
The va_list passed as dest should not be initialized by a previous call to va_start(), and must be passed to va_end()
before being reused as a parameter to va_start() or as the dest parameter of a subsequent call to va_copy(). The behavior is undefined should
any of these restrictions not be met.
The va_end() macro is used to clean up.
Multiple traversals, each bracketed by va_start and va_end, are possible.
|