Jump to content
  • Advertisement

Archived

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

zackriggle

unlimited parameters (like printf)

This topic is 5326 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 need to create a function that allows me to handle printf() functions, thus I need a function that can handle an infinite number of parameters. How do I go about doing this?

Share this post


Link to post
Share on other sites
Advertisement
Can you be more specific on why you need an invinite number of parameters to handle printf()? I can''t really think of a reason why this would be necessary.

You can''t have an infinite number of paramters (I''m guessing you''re using C or C++ since you''re using printf()). What you can do is pass a pointer to the function, and the pointer can point to an array.

Share this post


Link to post
Share on other sites
look up stdargs.h or varargs.h

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Whoa, cool! =) That definately seems like one of the less well-known C syntax tricks. =) I''ve certainly never seen it done before. Very neat. =)

Share this post


Link to post
Share on other sites
jenova: I would have, if I had known to look in those headers. I hadn't even the slightest idea where to start, or if "unlimited parameters" had some official name.

downgraded: thanks.

[edited by - zackriggle on January 18, 2004 5:35:09 PM]

Share this post


Link to post
Share on other sites
WARNING - passing C++ objects to a variadic function (that's how they are named) has undefined behaviour (a.k.a. "don't do this").


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on January 18, 2004 5:44:56 PM]

Share this post


Link to post
Share on other sites
int SomeFunc(int SomeInt, ...);

In SomeFunc:

char VarArgs[1024];
va_list Arg;
va_start(SomeInt, Arg);
vsprintf(VarArgs, SomeInt, Arg);
va_end(Arg);


In order to have a declare a methid which takes a variable parameter list the method must take at least one parameter.

After that code snippet above executes VarArgs will contain the variable arguments passed to the method

Share this post


Link to post
Share on other sites
The thing that is a bummer about varargs: the function needs to be told how many parameters there are.

So you either need to explicitly pass the number of arguments when calling it, or have it figure out the number implicitly (like printf can figure out how many arguments there are by looking at the string that you give it).

For me, that makes varargs not cool enough to ever use.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!