pass va_list

I want to pass va_list to another function, how can i achive this? Is it possible or not ?

Thanks & Regards
Karthick.R
Yes. For example vsnprintf() has a va_list parameter.
http://www.cplusplus.com/reference/cstdio/vsnprintf/
Thanks for your replay my question is different, below I give the sample code. For which I am expecting the solution.

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

void logger(const char *__format, ...) {
va_list __va;
va_start(__va, __format);
vprintf (__format, __va);
va_end(__va);
}

void my_logger(const char *__format, ... ) {
/* my business logic here */
logger(__format, ...); //----> How do u cal this function now ?
}

int main (int argc, char **argv)
{
my_logger("%d %s", 42, "Hello");
return 0;
}
First of all, do not use identifier starting with underscore! They are usually reserved for use by the people writing the library.

Secondly, why do you need the logger() function, when you could do everything in my_logger() instead? You are only making things harder for yourself.

As for your current code, you cannot just pass a variadic function's arguments to another variadic function.

In a function that is variadic (a function that has ellipsis ... as parameter) you construct the va_list and pass it to a function such as vprintf() which is not variadic.

So my suggestion is: use a single function my_logger() in which you construct the va_list and feed it to vprintf() just as you do in logger().

There is another approach which is ugly and is for the modern C language: variadic macros.
Transform my_logger() into a macro such as:

1
2
3
4
#define MY_LOGGER(format, ...)      do {    \
    /* my business logic here */            \
    logger(format, __VA_ARGS__);            \
} while (0) 


Another approach, reserved for modern C++: variadic function template, which I personally consider to be horrible.
http://www.cplusplus.com/articles/EhvU7k9E/
Topic archived. No new replies allowed.