Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write a varargs function that sends it argument list to another varargs function? [duplicate]

Tags:

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?

like image 978
gct Avatar asked Jan 13 '10 21:01

gct


People also ask

Can the fixed arguments passed to the function that accepts variable argument list occur at the end?

In a function that receives variable number of arguments the fixed arguments passed to the function can be at the end of argument list.

Can we pass a variable number of arguments to a function?

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.

How do you pass variable number of arguments in TypeScript?

The TypeScript / ECMAScript 6 Waylet varArgs = (... args: string[]) => { console. log(... args); };


1 Answers

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); } 
like image 192
John Dibling Avatar answered Sep 21 '22 13:09

John Dibling