From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/variadic%2Fva_copy.html | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 devdocs/c/variadic%2Fva_copy.html (limited to 'devdocs/c/variadic%2Fva_copy.html') diff --git a/devdocs/c/variadic%2Fva_copy.html b/devdocs/c/variadic%2Fva_copy.html new file mode 100644 index 00000000..fe57eb4c --- /dev/null +++ b/devdocs/c/variadic%2Fva_copy.html @@ -0,0 +1,56 @@ +

va_copy

Defined in header <stdarg.h>
void va_copy( va_list dest, va_list src );
+
(since C99)

The va_copy macro copies src to dest.

+

va_end should be called on dest before the function returns or any subsequent re-initialization of dest (via calls to va_start or va_copy).

+

Parameters

+ + +
dest - an instance of the va_list type to initialize
src - the source va_list that will be used to initialize dest

Expanded value

(none)

+

Example

#include <stdio.h>
+#include <stdarg.h>
+#include <math.h>
+ 
+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 < 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 < 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));
+}

Possible output:

+
0.920258

References

See also

+ + + + +
accesses the next variadic function argument
(function macro)
ends traversal of the variadic function arguments
(function macro)
holds the information needed by va_start, va_arg, va_end, and va_copy
(typedef)
enables access to variadic function arguments
(function macro)
C++ documentation for va_copy
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/variadic/va_copy +

+
-- cgit v1.2.3