va-a
rg
Iva-end
/va-sta
rt
Va
riable-Argu
ment
Macros/Function
Syntax
Description
Example
6-88
#include
<stdarg.h)
type
va-arg(ap,
type)
void
va-end(ap)
void
va-start
(ap,
parmN)
va-list
*ap
Some functions can be called
with
a varying number
of
arguments that have
varying types.
Such a function, called a variable-argument function, can
use the
following
macros
to
step through its argument list at run time. The
ap
parameter points
to
an
argument in the variable-argument list.
•
The
va-start
macro initializes
ap
to
point
to
the first argument in
an
argument list
for
the variable-argument function. The
parmN
param-
eter points
to
the rightmost parameter in the fixed, declared list.
•
The
va-arg
macro returns the value
of
the next argument
'in
a call
to
a variable-argument function.
Each
time you call
va-arg,
it
modifies
ap
so that successive arguments
for
the variable-argument
function
can be returned
by
successive calls
to
va-arg
(va-arg
modifies
ap
to
point
to
the next argument in the list). The type parameter
is
a type
name;
it
is
the type
of
the current argument in the list.
•
The
va-end
macro resets the stack environment after
va-start
and
va-arg
are
used.
Note that you must
call
va-start
to
initialize
ap
before calling
va-arg
or
va-end.
int
printf(fmt)
/*
Has
1
fixed
argument
and
*/
char
*fmt
/*
additional
variable
arguments
*/
int
i;
char
*s;
long
1;
va-list
ap;
va_start(ap,
fmt);
/*
initialize
*/
/*
Get
next
argument,
i
va-arg(ap,
int)
;
/*
Get
next
argument,
s
=
va-arg
(ap,
char
*)
;
/*
Get
next
argument,
1
va-arg
(ap,
long)
;
va-end(ap)
/*
Reset
*/
an
integer
*/
a
string
*/
a
long
*/