Namespaces
Variants
Views
Actions

va_start

From cppreference.com
< cpp‎ | utility‎ | variadic
Revision as of 11:40, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <cstdarg>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td class="t-dcl-nopad">
void va_start( va_list ap, parm_n );
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

The va_start macro enables access to the variable arguments following the named argument parm_n.

va_start should be invoked with an instance to a valid va_list object ap before any calls to va_arg.

Contents

Parameters

ap - an instance of the va_list type
parm_n - the named parameter preceding the first variable parameter

Expanded value

(none)

Example

#include <iostream>
#include <cstdarg>
 
int add_nums(int count, ...) 
{
    int result = 0;
    va_list args;
    va_start(args, count);
    for (int i = 0; i < count; ++i) {
        result += va_arg(args, int);
    }
    return result;
}
 
int main() 
{
    std::cout << add_nums(4, 25, 25, 50, 50) << '\n';
}

Output:

150

See also

accesses the next variadic function argument
(function macro) [edit]
ends traversal of the variadic function arguments
(function macro) [edit]