Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

va_copy — copying argument-list state

va_copy initializes independent va_list state at the source's current position, allowing separate reads of the same arguments.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdarg.h>
void va_copy(va_list dest, va_list src);

va_copy(dest, src) from <stdarg.h> initializes dest with the current state of a valid initialized src; it returns nothing. The copy does not restart. Reads through va_arg advance each list independently.

Use va_copy, not assignment or memcpy. Each va_start and va_copy requires a matching va_end in the same function. Before reinitializing an active dest, call va_end(dest).

The example skips 10 before copying, so both lists read 20. An int argument is read as int. The standard describes va_copy as a macro but permits an externally linked identifier implementation; do not suppress its macro definition.

Examples

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

static void show_next_twice(int count, ...)
{
    va_list src, dest;
    va_start(src, count);

    if (count >= 2) {
        (void)va_arg(src, int);
        va_copy(dest, src);

        int a = va_arg(src, int);
        int b = va_arg(dest, int);
        printf("%d %d\n", a, b);

        va_end(dest);
    }

    va_end(src);
}

int main(void)
{
    show_next_twice(3, 10, 20, 30);
    return 0;
}

Result

20 20

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources