C++ <cstdarg> - va_copy
The C++ <cstdarg> va_copy macro copies src to dest. After the call, it carries the information needed to retrieve the same additional arguments as src.
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).
Syntax
void va_copy( va_list dest, va_list src );
Parameters
dest |
Specify an instance of the va_list type to initialize. |
src |
Specify the source va_list that will be used to initialize dest. |
Return Value
None.
Example:
The example below shows the usage of va_copy macro function.
#include <iostream> #include <cstdarg> #include <cmath> using namespace std; double sample_stddev(int count, ...) { double sum = 0; va_list args1; va_list args2; va_start(args1, count); va_copy(args2, args1); //calculating mean for (int i = 0; i < count; ++i) { sum += va_arg(args1, double); } va_end(args1); double mean = sum / count; //calculating sum of square of difference 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); //returning sample standard deviation return sqrt(sum_sq_diff / count); } int main () { cout<<"Std. deviation is: "<<sample_stddev(2, 25.0, 30.0)<<"\n"; cout<<"Std. deviation is: "<<sample_stddev(3, 25.0, 30.0, 34.0)<<"\n"; cout<<"Std. deviation is: "<<sample_stddev(4, 25.0, 30.0, 34.0, 40.0)<<"\n"; return 0; }
The output of the above code will be:
Std. deviation is: 2.5 Std. deviation is: 3.68179 Std. deviation is: 5.49432
❮ C++ <cstdarg> Library