Passing variable arguments to another function that accepts a variable argument list
You can't do it directly; you have to create a function that takes a va_list
:
#include <stdarg.h>
static void exampleV(int b, va_list args);
void exampleA(int a, int b, ...) // Renamed for consistency
{
va_list args;
do_something(a); // Use argument a somehow
va_start(args, b);
exampleV(b, args);
va_end(args);
}
void exampleB(int b, ...)
{
va_list args;
va_start(args, b);
exampleV(b, args);
va_end(args);
}
static void exampleV(int b, va_list args)
{
...whatever you planned to have exampleB do...
...except it calls neither va_start nor va_end...
}
Maybe throwin a rock in a pond here, but it seems to work pretty OK with C++11 variadic templates:
#include <stdio.h>
template<typename... Args> void test(const char * f, Args... args) {
printf(f, args...);
}
int main()
{
int a = 2;
test("%s\n", "test");
test("%s %d %d %p\n", "second test", 2, a, &a);
}
At the very least, it works with g++
.
you should create versions of these functions which take a va_list, and pass those. Look at vprintf
as an example:
int vprintf ( const char * format, va_list arg );