index.txt
VARARGS(3) Library Functions Manual VARARGS(3) NAME varargs - variable argument list SYNOPSIS #include <varargs.h> function(va_alist) va_dcl va_list pvar; va_start(pvar); va_arg(pvar, type ); va_end(pvar); DESCRIPTION This set of macros allows portable procedures that accept variable ar‐ gument lists to be written. Routines which have variable argument lists (such as printf(3)) that do not use varargs are inherently non‐ portable, since different machines use different argument passing con‐ ventions. The literal identifier va_alist is used in a function header to declare a variable argument list. It is declared by va_dcl. Note that there is no semicolon after va_dcl. Va_list is the type of the variable pvar, which is used to traverse the list. One variable of this type must always be declared. Va_start initializes 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 is expected, since it cannot be determined at runtime. Va_end is used to finish up. Multiple traversals, each bracketed by va_start and va_end, are possi‐ ble. EXAMPLE How to define execl in terms of execv; see exec(2): #include <varargs.h> execl(va_alist) va_dcl { va_list ap; char *file; char *args[100]; int argno = 0; va_start(ap); file = va_arg(ap, char*); while (args[argno++] = va_arg(ap, char*)) continue; va_end(ap); execv(file, args); } VARARGS(3)