6.14.3 va_copy Macro

Copies a va_list object.

Include

<stdarg.h>

Prototype

void va_copy(va_list dest, va_list src);

Arguments

dest
the va_list object to be initialized
src
the va_list object to copy

Remarks

The function copies the src object into dest, such that the state of dest will reflect any call made to va_start to intialize src as well as the same sequence of va_arg macro calls that modified src.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#include <stdio.h>
#include <stdarg.h>
#include <limits.h>

/* print values as a percentage of the maximum value
 * list ends with 0 value
 */

void PrintAsPercent(int first, ...)
{
  char * buffer;
  int val = first;
  int valMax = INT_MIN;
  va_list vl_parse, vl_print;

  va_start(vl_parse, first);
  /* copy, after having called va_start */
  va_copy(vl_print, vl_parse);

  /* find the largest value */
  while(val != 0) {
    val = va_arg(vl_parse, int);
    if(val > valMax)
      valMax = val;
  }
  va_end(vl_parse);

  /* va_start not necessary with va_print */
  val = first;
  while(val != 0) {
    printf("%d%% ", val*100/valMax);
    val = va_arg(vl_print, int);
  }
  va_end(vl_print);
  printf("\n");
}

int main(void)
{
  PrintAsPercent(10, 23, 156, 42, 88, 0);
}

Example Output

6% 14% 100% 26% 56%