-

Functions HI-TECH C VA START

From HI-TECH C for CP/M Fan WIKI(EN)
Jump to: navigation, search

VA_START, VA_ARG, VA_END

SYNOPSIS

#include  <stdarg.h>

void      va_start(va_list ap, parmN);
type      va_arg(ap, type);
void      va_end(va_list ap);


DESCRIPTION

These macros are provided to give access in a portable way to parameters to a function represented in a prototype by the ellipsis symbol (...), where type and number of arguments supplied to the function are not known at compile time. The rightmost parameter to the function (shown as parmN) plays an important role in these macros, as it is the starting point for access to further parameters. In a function taking variable numbers of arguments, a variable of type va_list should be declared, then the macro va_start invoked with that variable and the name of parmN. This will initialize the variable to allow subsequent calls of the macro va_arg to access successive parameters. Each call to va_arg requires two arguments; the variable previously defined and a type name which is the type that the next parameter is expected to be. Note that any arguments thus accessed will have been widened by the default conventions to int, unsigned int or double. For example if a character argument has been passed, it should be accessed by va_arg(ap, int) since the char will have been widened to int. An example is given below of a function taking one integer parameter, followed by a number of other parameters. In this example the function expects the subsequent parameters to be pointers to char, but note that the compiler is not aware of this, and it is the programmers responsibility to ensure that correct arguments are supplied.

#include  <stdarg.h>
prf(int n, ...)
{
    va_list         ap;

    va_start(ap, n);
    while(n--)
    puts(va_arg(ap, char *));
    va_end(ap);
}