Passing stdargs

This topic is 4419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 on other sites
Are you working in C, or C++? I'm assuming C, because in C++ you shouldn't be using variadic functions or even char* strings for that matter.

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 on other sites
Quote:
 Original post by ZahlmanLessBread, 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 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 on other sites
Boost.Format. Unless you especially want to retain your buffer-overflow exploit vulnerability.

Σnigma

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631715
• Total Posts
3001868
×