HP-UX Reference (11i v1 05/09) - 5 Miscellaneous Topics (vol 9)
v
varargs(5) varargs(5)
NAME
varargs - handle variable argument list
SYNOPSIS
#include <varargs.h>
va_alist
va_dcl
void va_start(pvar)
va_list pvar;
type va_arg(pvar, type)
va_list pvar;
void va_end(pvar)
va_list pvar;
DESCRIPTION
This set of macros enables programmers to write portable procedures that accept variable argument lists.
Routines that have variable argument lists (such as printf()) but do not use varargs are inherently
nonportable, because different machines use different argument-passing conventions (see printf(3S)).
va_alist is used as the parameter list in a function header.
va_dcl is a declaration for va_alist . No semicolon should follow va_dcl.
va_list is a type defined for the variable used to traverse the list.
va_start is called to initialize pvar to the beginning of the list.
va_arg returns the next argument in the list pointed to by pvar. type is the type the argument is
expected to be. Different types can be mixed, but it is up to the routine to know what type of argument is
expected, because it cannot be determined at runtime.
va_end is used to clean up.
Multiple traversals, each bracketed by va_start ... va_end, are possible.
EXAMPLE
The following example shows a possible implementation of
execl() (see exec(2)):
#include <varargs.h>
#define MAXARGS 100
/* execl is called by
execl(file, arg1, arg2, ..., (char *)0);
*/
execl(va_alist)
va_dcl
{
va_list ap;
char *file;
char *args[MAXARGS];
int argno = 0;
va_start(ap);
file = va_arg(ap, char *);
while ((args[argno++] = va_arg(ap, char *)) != (char *)0);
va_end(ap);
return execv(file, args);
}
The next example illustrates how a function that receives variable arguments can pass these arguments
down to other functions. To accomplish this, the first routine (log_errors() in this example) which
receives the variable argument list must pass the address pointer resulting from a call to va_start()
on to any subsequent calls that need to access this same variable argument list. All routines that receive
this address pointer (v_print_log() in this example) need only to use va_arg() to access the origi-
nal variable argument list just as if they were the original routine to be passed the variable arguments.
Section 5−−382 Hewlett-Packard Company − 1 − HP-UX 11i Version 1: September 2005