HP C/iX Reference Manual (31506-90011)
162 Chapter9
HP C/iX Implementation Topics
The varargs Macros
va_start(ap);
/* Get the first argument, and arg list flag */
a1 = va_arg (ap, int);
a2 = va_arg (ap, enum arglisttype);
printf ("arg count = %d\n", a1);
if (a2 == VAR_LIST_PRESENT) {
/* pick up all the arguments */
do {
/* get the type of the argument */
ptype = va_arg (ap, enum argtype);
/* retrieve the argument based on the type */
switch (ptype) {
case CHAR: c = va_arg (ap, char);
printf ("char = %c\n", c);
break;
case DOUB: d = va_arg (ap, double);
printf ("double = %f\n", d);
break;
case PINT: p = va_arg (ap, int *);
printf ("pointer = %x\n", p);
break;
case INT : i = va_arg (ap, int);
printf ("int = %d\n", i);
break;
case END_OF_LIST :
break;
default: printf ("bad argument type %d\n", ptype);
ptype = END_OF_LIST; /* to break loop */
break;
} /* switch */
} while (ptype != END_OF_LIST);
}
/* Clean up */
va_end (ap);
}
main()
{
int x = 99;
foo (1, NO_VAR_LIST);
foo (2, VAR_LIST_PRESENT, DOUB, 3.0, PINT, &x, END_OF_LIST);
}