Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variable arguments to another function

Status
Not open for further replies.

greeneka

Programmer
Aug 11, 2003
17
IE
Hi All,

I'm writing a debug logging function and was wondering if there's any way to pass variable arguments straight to another function without touching them.

For example I want to do something like this:

Code:
#include <stdio.h>

const int maxlevel = 3;
void debugLog(int debuglevel, char *msg, ...);

void main(){
	debugLog(2, &quot;should be printed%d&quot;, 5);
	debugLog(5, &quot;should not be printed%d&quot;, 8);
}

void debugLog(int debuglevel, char *msg, ...){
	if(debuglevel < maxlevel){
		printf(msg, ...);
	}
}

This doesn't compile, but you can see the idea - passing the var args straight through the debugLog function to the printf function without having to touch them.

Any help or pointers would be much appreciated.

Thanks in advance.
 
>> any way to pass variable arguments straight to another
>> function without touching them.

No.


-pete
 
Seems easy enough...
Code:
#include <stdio.h>
#include <stdarg.h>

const int maxlevel = 3;
void debugLog(int debuglevel, char *msg, ...);

int main ( ) { //!! main returns int
    debugLog(2, &quot;should be printed %d\n&quot;, 5);
    debugLog(5, &quot;should not be printed %d\n&quot;, 8);
    return 0;
}

void debugLog(int debuglevel, char *msg, ...){
    va_list ap;
    va_start(ap,msg);
    if(debuglevel < maxlevel){
        vprintf(msg, ap);
    }
    va_end(ap);
}

--
 
Salem,

Oh right! You didn't touch them, you used your keyboard [lol]

-pete
 
Thanks for the replies.

Salem - The example I gave was contrived, it's not printf I'm calling but a different function that takes variable arguments that does more than just print to the console (I just thought printf would be the easiest example). So I was hoping to be able to modify my debug code and still be able to call the same function.
I *could* split the function I want to call in two (one taking variable arguments, and the other taking a va_list) but I'd rather not!

Palbano - After some digging around last night I think your answer is (unfortunately!) the one I needed. For those interested, I came accross this while trying to figure it out:

Apparently this problem has already been named the &quot;inverse varargs problem&quot;!

Anyway, thanks for the help.
 
Note that in a world of overloaded operators and functions specified by their parameters, the ... solution is, well, obsolete.


/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
P.S. Apologies - I meant to post the solution I'm using:

I vsprintf'ed the data into a buffer and used that:
Code:
void debugLog(int debuglevel, char *msg, ...){
    if(debuglevel < maxlevel){
 		char buffer[500];
		va_list argptr;
		va_start(argptr,msg);
		vsprintf( buffer, msg, argptr ); 
		printf(buffer); // well not printf but you get the idea!:)
	}
}
 
PerFnurt - true, but I have to call a previously defined function that uses the ellipsis, so I have to work with it!
 
> it's not printf I'm calling but a different function that takes variable arguments
I would say that the &quot;proper&quot; answer would be to make it all more printf-like.

Eg. if you have
Code:
void foo ( char *msg, ... );

then you should also have
Code:
void vfoo ( char *msg, va_list ap );

Where foo is actually implemented as a simple wrapper for calling vfoo(), and vfoo() does all the work foo() was doing anyway.
Code:
void foo ( char *msg, ... ) {
  va_list ap;
  va_start(ap,msg);
  vfoo(msg,ap);
  va_end(ap);
}

Then from the application, you can call foo() for convenience, and vfoo() if necessary.

If you don't own foo(), then go and hassle the author to provide this interface :)

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top