Possible Duplicate:
C/C++: Passing variable number of arguments around
Imagine I have a function mySuperDuperPrintFunction that takes a variable number of arguments. This function calls printf (or any other function with a variable number of arguments. Can i somehow pass all, or only some, parameters from the arglist to the other function ? Like
void mySuperDuperPrintFunction(char* text, ...) {
/*
* Do some cool stuff with the arglist.
*/
// Call printf with arguments from the arglist
printf(text, *someWayToExtractTheArglist());
}
Yes:
You can find info about
vprintfhere (or check your man pages).I’ve done similar for wrapping functions such as
write(Unix) andOutputDebugString(Windows) so that I could create a formatted string to pass to them withvsnprintf.EDIT: I misunderstood your question. No you can’t, at least not in C. If you want to pass the variable number of arguments on, the function you’re calling must take a
va_listargument, just likevprintfdoes. You can’t pass yourva_listonto a function such asprintf(const char *fmt,...). This link contains more information on the topic.If the function does take a
va_listargument, then you can pass arguments from a specific point (ie. you might to skip the first one). Retrieving arguments withva_argwill update theva_listpointer to the next argument, and then when you pass theva_listto the function (such asvprintf), it’ll only be able to retrieve arguments from that point on.