4.3BSD-Reno/share/man/cat3/varargs.0
VARARGS(3) 1990 VARARGS(3)
NNAAMMEE
varargs - variable argument lists
SSYYNNOOPPSSIISS
##iinncclluuddee <<ssttddaarrgg..hh>>
vvooiidd
vvaa__ssttaarrtt((vvaa__lliisstt aapp,, llaasstt));;
ttyyppee
vvaa__aarrgg((vvaa__lliisstt aapp,, ttyyppee));;
vvooiidd
vvaa__eenndd((vvaa__lliisstt aapp));;
DDEESSCCRRIIPPTTIIOONN
A function may be called with a varying number of arguments
of varying types. The include file <_s_t_d_a_r_g._h> declares a
type (_v_a__l_i_s_t) and defines three macros for stepping through
a list of arguments whose number and types are not known to
the called function.
The called function must declare an object of type _v_a__l_i_s_t
which is used by the macros _v_a__s_t_a_r_t, _v_a__a_r_g, and _v_a__e_n_d.
The _v_a__s_t_a_r_t macro initializes _a_p for subsequent use by
_v_a__a_r_g and _v_a__e_n_d, and must be called first.
The parameter _l_a_s_t is the name of the last parameter before
the variable argument list, i.e. the last parameter of which
the calling function knows the type.
Because the address of this parameter is used in the
_v_a__s_t_a_r_t macro, it should not be declared as a register
variable, or as a function or array type.
The _v_a__s_t_a_r_t macro returns no value.
The _v_a__a_r_g macro expands to an expression that has the type
and value of the next argument in the call. The parameter
_a_p is the _v_a__l_i_s_t _a_p initialized by _v_a__s_t_a_r_t. Each call to
_v_a__a_r_g modifies _a_p so that the next call returns the next
argument. The parameter _t_y_p_e is a type name specified so
that the type of a pointer to an object that has the speci-
fied type can be obtained simply by adding a * to _t_y_p_e.
If there is no next argument, or if _t_y_p_e is not compatible
with the type of the actual next argument (as promoted
according to the default argument promotions), random errors
will occur.
Printed 7/27/90 May 1
VARARGS(3) 1990 VARARGS(3)
The first use of the _v_a__a_r_g macro after that of the _v_a__s_t_a_r_t
macro returns the argument after _l_a_s_t. Successive invoca-
tions return the values of the remaining arguments.
The _v_a__e_n_d macro handles a normal return from the function
whose variable argument list was initialized by _v_a__s_t_a_r_t.
The _v_a__e_n_d macro returns no value.
SSTTAANNDDAARRDDSS
The _v_a__s_t_a_r_t, _v_a__a_r_g, and _v_a__e_n_d macros are ANSI C compati-
ble.
CCOOMMPPAATTIIBBIILLIITTYY
These macros are nnoott compatible with the historic macros
they replace. A backward compatible version can be found in
the include file <_v_a_r_a_r_g_s._h>.
EEXXAAMMPPLLEESS
The function _f_o_o takes a string of format characters and
prints out the argument associated with each format charac-
ter based on the type.
foo(fmt)
char *fmt;
{
va_list ap;
int d;
char c, *p, *s;
va_start(ap, fmt);
while (*fmt)
switch(*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case 'c': /* char */
c = va_arg(ap, char);
printf("char %c\n", c);
break;
}
va_end(ap);
}
Printed 7/27/90 May 2