I’m trying to write a function that accepts a variable number of parameters like printf, does some stuff, then passes the variable list to printf. I’m not sure how to do this, because it seems like it would have to push them onto the stack.
Something approximately like this
#include <stdio.h>
#include <stdarg.h>
void forward_args( const char *format , ... ){
va_list arglist;
printf( format, arglist );
}
int main (int argc, char const *argv[]){
forward_args( "%s %s\n" , "hello" , "world" ); return 0;
}
Any ideas?
Don’t pass the results to
printf. pass them tovprintf.vprintfspecifically exists to handle passing inva_listarguments. From the Linux man page:Notice how the latter explicitly take
va_listarguments such as the ones you declare inside a function taking...in the parameter list. So your function would be declared like this: