beautypg.com

Va_end, Returns, Example – Zilog ZUSBOPTS User Manual

Page 482

background image

Standard Functions

UM017105-0511

454

Zilog Developer Studio II – ZNEO™
User Manual

Synopsis

#include

type va_arg(va_list ap, type);

Returns

The first invocation of the

va_arg

macro after that of the

va_start

macro returns the

value of the argument after that specified by parmN. Successive invocations return the val-
ues of the remaining arguments in succession.

Example

The function f1 gathers into an array a list of arguments that are pointers to strings (but not
more than MAXARGS arguments), then passes the array as a single argument to function
f2. The number of pointers is specified by the first argument to f1.

#include

extern void f2(int n, char *array[]);

#define MAXARGS 31

void f1(int n_ptrs,...) {

va_list ap;

char *array[MAXARGS];

int ptr_no = 0;

if (n_ptrs > MAXARGS)

n_ptrs

=

MAXARGS;

va_start(ap, n_ptrs);

while (ptr_no < n_ptrs)

array[ptr_no++] = va_arg(ap, char *);

va_end(ap);

f2(n_ptrs, array);

}

Each call to f1 has in scope the definition of the function of a declaration such as

void

f1(int, ...);

va_end

Facilitates a normal return from the function whose variable argument list was referenced
by the expansion of

va_start

that initialized the

va_list ap

. The

va_end

function

can modify

ap

so that it is no longer usable (without an intervening invocation of

va_start

). If the

va_end

function is not invoked before the return, the behavior is unde-

fined.

This manual is related to the following products: