Advertisement

The "..." variable thing

Started by August 27, 2000 03:17 AM
1 comment, last by Sponge99 24 years, 4 months ago
I wanted to know..... In a function like printf, it uses the "..." to accept as many parameters as you want. I was wondering how to do this in my own functions. I know that you just do void Fubar(char s, ...) { } but how do I access the "..." part. If i say printf(...) it''ll work, but then it just types a bunch of garbage. Is it an array, or what? Help! Thanx! +H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
"Now watch as I run away in a womanly fashion." - Batman
You access it like this:

    va_list arg_list; // arguments listchar buffer[128]; // some temp bufferint Print_Text(char *text,...){        va_start(arg_list,text); // get the arguments after the text parameter	vsprintf(buffer,text,arg_list); // use the ... paramteres	va_end(arg_list); // end it}    


This way you can Print_Text("Number: %d",SomeNumber);
You get the idea.



The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Advertisement
In this case, arg_list is an array of pointers to void... which means that you have no way to know the type of each arguments... The only way is to put a way to pass up the arguments type through the "normal" arguments...

The help files of MSVC and BC++ are really good on variable parameters, you should read them.

BTW, don''t forget that this argument may be null, meaning that the user haven''t passed anything.


Programming is: A.The art of debugging a blank sheet of paper (or an empty file).B.A pastime similar to banging one's head against a wall, but with fewer opportunities for reward.C.The most fun you can have with your clothes on (although clothes are not mandatory).
Now I know what I'm made of, and I'm afraid of it...

This topic is closed to new replies.

Advertisement