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;
}
Passing stdargs
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.
I'm not really sure what keywords to google for this sort of thing, so I figured I'd ask here.
Thanks :)
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.
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.
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.
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)"...
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.
@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 :)
Boost.Format. Unless you especially want to retain your buffer-overflow exploit vulnerability.
Σnigma
Σnigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement