HP C A.06.05 Reference Manual

HP C/HP-UX Implementation Topics
The varargs Macros
Chapter 10 253
The varargs Macros
The varargs macros allow accessing arguments of functions where the number and types of
the arguments can vary from call to call.
NOTE The <varargs.h> header has been superseded by the standard header
<stdarg.h>, which provides all the functionality of the varargs macros. The
<varargs.h> header is retained for compatibility with pre-ANSI compilers and
earlier releases of HP C/HP-UX.
To use varargs, a program must include the header <varargs.h>. A function that expects a
variable number of arguments must declare the first variable argument as va_alist in the
function declaration. The macro va_dcl must be used in the parameter declaration.
A local variable should be declared of type va_list. This variable is used to point to the next
argument in the variable argument list.
The va_start macro is used to initialize the argument pointer to the initial variable
argument.
Each variable argument is accessed by calling the va_arg macro. This macro returns the
value of the next argument, assuming it is of the specified type, and updates the argument
pointer to point to the next argument.
The va_end macro is provided for consistency with other implementations; it performs no
function on HP systems. The following example demonstrates the use of the <varargs.h>
header:
Example
#include <varargs.h>
#include <stdio.h>
enum arglisttype {NO_VAR_LIST, VAR_LIST_PRESENT};
enum argtype {END_OF_LIST, CHAR, DOUB, INT, PINT};
int foo (va_alist)
va_dcl /* Note: no semicolon */
{
va_list ap;
int a1;