beautypg.com

Va_start – Zilog Z8F0130 User Manual

Page 500

background image

Appendix B. C Standard Library

UM013037-1212

476

Zilog Developer Studio II – Z8 Encore!
User Manual

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_start

Is executed before any access to the unnamed arguments.

The parameter

ap

points to an object that has type

va_list

. The parameter

parmN

is the

identifier of the rightmost parameter in the variable parameter list in the function defini-
tion (the one just before the , ...). The

va_start

macro initializes

ap

for subsequent use

by

va_arg

and

va_end

.

Synopsis

#include

void va_start(va_list ap, parmN);

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);

}