How do I fill a va_list

Normally, these functions come in pairs. If you have a "va-accepting" function, it is easy to create another one:

void printInts_v(int n, va_list ap)
{
    unsigned int i=0;
    for(i=0; i<n; i++)
    {
        int arg=va_arg(ap, int);
        printf("%d", arg);
    }
}

This function can be called this way:

void printInts(int n,...)
{
    va_list ap;
    va_start(ap, n);
    printInts_v(n, ap);
    va_end(va);
}

But I don't think there is a way to portably pre-fill a va_list for later use.

If you work on one architecture and portability is not an issue, you could craft something on your own, however. How exactly to do that is platform-specific.


There's no ability to fill a va_list explicitly.

You should write a wrapper function. Say you need to call your function foo, instead of manually filling in a va_list, you define a new function like so:

void call_foo(int arg1, ...)
{
   va_list ap;
   va_start(ap, arg1);
   foo(arg1, ap);
   va_end(ap);
}

Now you can call foo, which takes a va_list, however you like, by doing e.g. call_foo(1,2,3,4);, call_foo(1, 1, "Hello"); etc.

This will only allow you to specify the arguments at compile time, you can't build the arguments at runtime.