summaryrefslogtreecommitdiff
path: root/devdocs/c/variadic%2Fva_copy.html
blob: fe57eb4c2a345f225c5cf73b0a8eaef9a88a82c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    <h1 id="firstHeading" class="firstHeading">va_copy</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdarg.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">void va_copy( va_list dest, va_list src );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <p>The <code>va_copy</code> macro copies <code>src</code> to <code>dest</code>.</p>
<p><code>va_end</code> should be called on <code>dest</code> before the function returns or any subsequent re-initialization of <code>dest</code> (via calls to <code>va_start</code> or <code>va_copy</code>).</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> dest </td> <td> - </td> <td> an instance of the <code>va_list</code> type to initialize </td>
</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> the source <code>va_list</code> that will be used to initialize <code>dest</code> </td>
</tr>
</table> <h3 id="Expanded_value"> Expanded value</h3> <p>(none)</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;stdarg.h&gt;
#include &lt;math.h&gt;
 
double sample_stddev(int count, ...) 
{
    /* Compute the mean with args1. */
    double sum = 0;
    va_list args1;
    va_start(args1, count);
    va_list args2;
    va_copy(args2, args1);   /* copy va_list object */
    for (int i = 0; i &lt; count; ++i) {
        double num = va_arg(args1, double);
        sum += num;
    }
    va_end(args1);
    double mean = sum / count;
 
    /* Compute standard deviation with args2 and mean. */
    double sum_sq_diff = 0;
    for (int i = 0; i &lt; count; ++i) {
        double num = va_arg(args2, double);
        sum_sq_diff += (num-mean) * (num-mean);
    }
    va_end(args2);
    return sqrt(sum_sq_diff / count);
}
 
int main(void) 
{
    printf("%f\n", sample_stddev(4, 25.0, 27.3, 26.9, 25.7));
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">0.920258</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.16.1.2 The va_copy macro (p: 270) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.15.1.2 The va_copy macro (p: 250) </li></ul>
</ul>      <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="va_arg" title="c/variadic/va arg"> <span class="t-lines"><span>va_arg</span></span></a></div> </td> <td> accesses the next variadic function argument <br> <span class="t-mark">(function macro)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="va_end" title="c/variadic/va end"> <span class="t-lines"><span>va_end</span></span></a></div> </td> <td> ends traversal of the variadic function arguments <br> <span class="t-mark">(function macro)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="va_list" title="c/variadic/va list"> <span class="t-lines"><span>va_list</span></span></a></div> </td> <td> holds the information needed by va_start, va_arg, va_end, and va_copy <br> <span class="t-mark">(typedef)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="va_start" title="c/variadic/va start"> <span class="t-lines"><span>va_start</span></span></a></div> </td> <td> enables access to variadic function arguments <br> <span class="t-mark">(function macro)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/utility/variadic/va_copy" title="cpp/utility/variadic/va copy">C++ documentation</a></span> for <code>va_copy</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/variadic/va_copy" class="_attribution-link">https://en.cppreference.com/w/c/variadic/va_copy</a>
  </p>
</div>