macro
<cstdarg>

va_copy

void va_copy (va_list dest, va_list src);
Copy variable argument list
Initializes dest as a copy of src (in its current state).

The next argument to be extracted from dest is the same as the one that would be extracted from src.

A function that invokes va_copy, shall also invoke va_end on dest before it returns.

Parameters

dest
Uninitialized object of type va_list.
After the call, it carries the information needed to retrieve the same additional arguments as src.
If dest has already been passed as first argument to a previous call to va_start or va_copy, it shall be passed to va_end before calling this function.
src
Object of type va_list that already carries information to retrieve additional arguments with va_arg (i.e., it has already been passed as first argument to va_start or va_copy ans has not yet been released with va_end).

Return Value

none

Example

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
/* va_copy example */
#include <stdio.h>      /* printf, vprintf*/
#include <stdlib.h>     /* malloc */
#include <string.h>     /* strlen, strcat */
#include <stdarg.h>     /* va_list, va_start, va_copy, va_arg, va_end */

/* print ints until a zero is found: */
void PrintInts (int first,...)
{
  char * buffer;
  const char * format = "[%d] ";
  int count = 0;
  int val = first;
  va_list vl,vl_count;
  va_start(vl,first);
  
  /* count number of arguments: */
  va_copy(vl_count,vl);
  while (val != 0) {
    val=va_arg(vl_count,int);
    ++count;
  }
  va_end(vl_count);
  
  /* allocate storage for format string: */
  buffer = (char*) malloc (strlen(format)*count+1);
  buffer[0]='\0';
  
  /* generate format string: */
  for (;count>0;--count) {
    strcat (buffer,format);
  }
  
  /* print integers: */
  printf (format,first);
  vprintf (buffer,vl);
  
  va_end(vl);
}

int main ()
{
  PrintInts (10,20,30,40,50,0);
  return 0;
}


Output:
[10] [20] [30] [40] [50] [0]

See also