va_start


Initialization of variable for scanning argument list

[Classification]

Standard library

[Syntax]

#include <stdarg.h>

void va_start(va_list ap, last-named-argument);

[Description]

This function initializes variable ap so that it indicates the beginning (argument next to last-named-argument) of the list of the variable arguments.

To define function func having a variable arguments in a portable form, the following format is used.

#include    <stdarg.h>
void func(arg-declarations, ...) {
        va_list ap;
        type    argN;
        va_start(ap, last-named-argument);
        argN = va_arg(ap, type);
        va_end(ap);
}

Remark

arg-declarations is an argument list with the last-named-argument declared at the end. ", ..." that follows indicates a list of the variable arguments. va_listis the type of the variable (ap in the above example) used to scan the argument list.

[Example]

#include    <stdarg.h>
void abc(int first, int second, ...) {
        va_list ap;
        int     i;
        char    c, *fmt;
        va_start(ap, second);
        i = va_arg(ap, int);
        c = va_arg(ap, int);        /*char type is converted into int type.*/
        fmt = va_arg(ap, char *);
        va_end(ap);
}