stdarg.h - Arguments of variadic functions

This page was translated by a robot.

In the C and C++ languages ​​it is possible to leave open the exact number of additional arguments expected and their type in the parameter list of a function with at least one parameter by means of three ellipses . ...Depending on the situation, more or less arbitrary arguments can be passed to such functions, which are designated as variadic , without the programmer having to program a new signature for every conceivable case. stdargThe library is required to read the passed arguments.

C
C++
#include <stdarg.h>
#include <cstdarg>

Definitions in the Library

The use of variadic functions is part of the C and C++ language, but there are no special keywords or other syntactical elements to address the additional arguments directly. By including the stdarglibrary, however, the programmer has a few macros available which enable the arguments to be addressed. Also in this library a type is defined for an argument list:

List Type
va_list

This type stores a pointer to one of the additional arguments. After a variable is defined with this type, it must be ...initialized to the last fixed parameter before the ellipsis. This is done by calling the va_startmacro:

Initialize List
va_start(argumentlist, lastfixedparameter)

va_argAfter initialization, each additional argument can now be read out individually by repeated use of the macro. After each application, the argument list variable automatically points to the next argument. In order for this to be possible, the macro also needs a type to determine the size of the variable.

Get Argument
va_arg(argumentlist, typeofargument)

After all desired parameters have been read, the argument list variable must be cleared. This is va_endachieved using the macro:

Destroy List
va_end(argumentlist)

The above macros are sufficient for simple processing of the argument list. However, if the argument list has to be processed several times or if the entire argument list is to be passed to another function, the macro is also va_copyrequired, which duplicates the argument list variable:

Duplicate List
va_copy(destinationlist, sourcelist)

Since this library is not a collection of functions but a part of the language C and C++, this listing of the defined macros and the type should suffice here on this page. For a detailed description of how to use variadic functions and some of the problems associated with them, see the function parameter... page.