How would I write a function with an undefined number of arguments?
Like, how when using printf(), you write the string in the first argument and in the string use format identifiers. Then, for each format identifier you use is an argument specifying what should be put there in the string.
How can I do this in my own function?
I wrote my own char* concatenating function, and it would be nice if I didn't have to cc(cc(cc(cc(somevalue, another), someothervaule), somethingelse), yetmoredata);
if I need to group more than two things together. I know I could re-define the function with two, three, four, five arguments but I would really prefer to do it this way.
winrawr
9
Junior Poster
Recommended Answers
Jump to PostThere are three ways to do this:
(a) use the printf style// Note thist must have at least one fixed argument: void foo(int flag,...) { va_list ap; va_start(ap, flag); if (flag) { char* s = va_arg(ap, char *); double d=va_arg(ap,double); std::cout<<"s == "<<s<<" "<<d<<std::endl; va_end(ap); } …
All 4 Replies
StuXYZ
731
Practically a Master Poster
ArkM
1,090
Postaholic
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
ArkM
1,090
Postaholic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.