Sign in to follow this  
MatrixCubed

Passing stdargs

Recommended Posts

Hello all, In my game engine I found it necessary to have one class utilize the stdarg iterator found inside another class. I would like to pass the argument list as-is, without processing it (or specifically, without duplicating code unnecessarily), however my dilemma is I don't know how to pass the arguments. An example follows.
void do_stuff(char* str, ...)
{
	char complete_str[2048];

	// Compose the text string
	va_list ap;

 	va_start(ap, str);
  	vsprintf(complete_str, str, ap);
  	va_end(ap);
}


void do_other_stuff(char* string, ...)
{
  	do_stuff(/* WHAT GOES HERE??? */);
}


int main(void)
{
	char* str = "string";
	do_other_stuff("this is a %s", str);

	return 0;
}

I'm not really sure what keywords to google for this sort of thing, so I figured I'd ask here. Thanks :)

Share this post


Link to post
Share on other sites
It doesn't work that way, at least, not without tweaking. The difficulty is that the number of arguments passed is determined at compile time, not run time, so there's no way to know exactly "what goes there".

My response to this newsgroup question might help (but it might not [grin]): Calling a function with a dynamic number of parameters. YMMV. That's in C, but with a bit of tweaking it should work with C++ too.

I bet if you wait a bit, a C++ guru will come along with a way that you can accomplish what you want in C++ without having to use any assembly language tweaks.

Share this post


Link to post
Share on other sites
LessBread, somehow I doubt he's trying to use, or interested in using, C++. Maybe it's the fact that he's trying to solve this problem, or even using char* to represent text in the first place; maybe it's the way he writes "int main(void)"...

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
LessBread, somehow I doubt he's trying to use, or interested in using, C++. Maybe it's the fact that he's trying to solve this problem, or even using char* to represent text in the first place; maybe it's the way he writes "int main(void)"...


Well, he started off talking about classes, so C++ seemed to follow from that. If he's not interested in using C++, then the link I dropped should be helpful.

Share this post


Link to post
Share on other sites

@LessBread: Hmm doesn't really simplify things, which is really what I was aiming for :) Thanks for the link though (which incidentally doesn't like Firefox), it's going into The Vault. :)

@ApochPiQ: I use features of either language where appropriate. The code from my engine which prompted me to ask in this thread is for a log() function which simply takes a variable list of arguments, and pumps them into a single string. It might not be politically correct, but my personal preference is printf() structuring over iostream.

@Zahlman: You're right, I simply wrote dummy code which focused on my actual problem, as opposed to copy-pasting 1k lines of code from my engine.

Thanks all :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this