|
VMS Help CC, Language topics, Variable Length Argument Lists *Conan The Librarian |
The set of functions and macros defined and declared in the
<varargs.h> and the <stdarg.h> header files provide a method of
accessing variable-length argument lists. (Note that the
<stdarg.h> functions are defined by the ANSI C standard and are,
therefore, portable as compared with those defined in <varargs.h>.)
The HP C RTL functions such as printf and execl, for example, use
variable-length argument lists. User-defined functions with
variable-length argument lists that do not use <varargs.h> or
<stdarg.h> are not portable due to the different argument-passing
conventions of various machines.
To use these functions and macros in <stdarg.h>, you must include
the <stdarg.h> header file with the following preprocessor
directive:
#include <stdarg.h>
The <stdarg.h> header file declares a type (va_list) and three
macros (va_start, va_arg, and va_end) for advancing through a list
of function arguments of varying number and type. The macros have
the following syntax:
void va_start(va_list ap, parmN);
type va_arg(va_list ap, type);
void va_end(va_list ap);
The va_start macro initializes the object ap of type va_list 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.
The va_arg macro expands to an expresion that has the type and
value of the next argument in the call. The parameter ap is the
same as the one 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". 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 behavior is undefined. 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.
The va_end macro 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.
|
|