varargs.5 (2010 09)

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. The type of argN should be the same as
the argument to the function just before the variable portion of the argument 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.
NOTE: The <
varargs.h> header file is provided for compatibility with pre-ANSI compilers and earlier
releases of HP C/HP-UX. It is superceded by <stdarg.h> which includes all of the varargs
macros.
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
HP-UX 11i Version 3: September 2010 1 Hewlett-Packard Company 1

Summary of content (4 pages)