• Advertisement

Archived

This topic is now archived and is closed to further replies.

The "..." variable thing

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
You access it like this:

    
va_list arg_list; // arguments list

char buffer[128]; // some temp buffer


int 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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

  • Advertisement