-

Changes

Jump to: navigation, search

Functions HI-TECH C VA START

1,985 bytes added, 00:26, 1 August 2017
Created page with "<strong>VA_START, VA_ARG, VA_END</strong> ==SYNOPSIS== #include <stdarg.h> void va_start(va_list ap, <strong>parmN</strong>); <strong>type</strong> va_arg(ap,..."
<strong>VA_START, VA_ARG, VA_END</strong>
==SYNOPSIS==

#include <stdarg.h>

void va_start(va_list ap, <strong>parmN</strong>);
<strong>type</strong> va_arg(ap, <strong>type</strong>);
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 <strong>parmN</strong>) 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 <strong>parmN</strong>. 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 <strong>int, unsigned int</strong> or <strong>double</strong>. 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 <strong>int</strong>. 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);
}

Navigation menu