Possible Duplicate:
C Programming: Forward variable argument list.
What I'd like to do is send data to a logging library (that I can't modfify) in a printf kind of way.
So I'd like a function something like this:
void log_DEBUG(const char* fmt, ...) { char buff[SOME_PROPER_LENGTH]; sprintf(buff, fmt, <varargs>); log(DEBUG, buff); }
Can I pass varargs to another vararg function in some manner?
In a function that receives variable number of arguments the fixed arguments passed to the function can be at the end of argument list.
When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit.
The TypeScript / ECMAScript 6 Waylet varArgs = (... args: string[]) => { console. log(... args); };
You can't forward the variable argument list, since there's no way to express what's underneath the ...
as a parameter(s) to another function.
However you can build a va_list
from the ...
parameters and send that to a function which will format it up properly. This is what vsprintf
is for. Example:
void log_DEBUG(const char* fmt, ...) { char buff[SOME_PROPER_LENGTH]; va_list args; va_start(args, fmt); vsprintf(buff, fmt, args); va_end(args); log(DEBUG, buff); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With