Calling function with a variable number of arguments from a function with a variable number of arguments

Possible duplicate:
C / C ++: passing a variable number of arguments around

Suppose 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 another function? How

 void mySuperDuperPrintFunction(char* text, ...) { /* * Do some cool stuff with the arglist. */ // Call printf with arguments from the arglist printf(text, *someWayToExtractTheArglist()); } 
+4
source share
1 answer

Yes:

 va_list args; va_start(args, text); vprintf(format_goes_here, args); 

You can find information on vprintf here (or check your manual pages).

I did the same for wrapper functions like write (Unix) and OutputDebugString (Windows) so that I could create a formatted string to pass them using vsnprintf .

EDIT: I misunderstood your question. No, you cannot, at least not in C. If you want to pass a variable number of arguments, the function you call must accept the va_list argument, as vprintf does. You cannot pass your va_list to a function like printf(const char *fmt, ... ) . This link contains additional information on this topic.

If the function accepts the va_list argument, then you can pass arguments from a specific point (i.e. you can skip the first). Returning the arguments with va_arg will update the va_list pointer to the next argument, and then when you pass va_list to a function (e.g. vprintf ), it can only retrieve the arguments from this point on.

+2
source

Source: https://habr.com/ru/post/1380282/


All Articles