9.9 Variable Arguments (<stdarg.h>)

The <stdarg.h> header file declares a type and defines three macros for advancing through a list of function arguments of varying number and type.

Type

va_list


A type suitable for holding information needed by the macros va_start , va_arg , and va_ end .

To access varying arguments, the called function must declare an object (referred to as ap in this section) that has the type va_list :

va_list ap;

The object ap can be passed as an argument to another function. If that function invokes the va_arg macro with parameter ap, the value of ap in the calling function is indeterminate and is passed to the va_ end macro before any further reference to ap.

Macros

void va_start(va_list ap, parmN);


Initializes ap for subsequent use by va_ arg and va_end . The va_start macro must be invoked before any access to the unnamed arguments.

The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition. If parmN is declared with the register storage class, with a function or array type, or with a type that is not compatible with the type that results after application of the default arguments promotions, the behavior is undefined. The va_start macro returns no value.

type va_arg(va_list ap, type);


Expands to an expression that has the type and value of the next argument in the call. The parameter ap is the same as the va_list ap that was initialized by va_start . Each invocation of va_arg modifies ap so that the values of successive arguments are returned in turn. The parameter type is a type name specified such that the type of a pointer to an object that has the specified type can be obtained by postfixing an asterisk (*) to type. The behavior is undefined if there is no actual next argument, or if type is not compatible with the type of the next actual argument (as promoted according to the default argument promotions).

The first invocation of va_arg after that of va_start returns the value of the argument after that specified by parmN. Successive invocations return the values of the remaining arguments in turn.

void va_end(va_list ap);


Facilitates a normal return from the function whose variable argument list was referred to by the expansion of va_start that initialized the va_list ap object. The va_end macro can modify ap so that it can no longer be used (without an intervening invocation of va_start ). If there is no corresponding invocation of va_start or if va_ end is not invoked before the return, the behavior is undefined. The va_end macro returns no value.


Previous Page | Next Page | Table of Contents | Index