Archived

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

zackriggle

unlimited parameters (like printf)

Recommended Posts

zackriggle    130
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
nosfuratu    122
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
jenova    122
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
zackriggle    130
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
Fruny    1658
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
Spudder    385
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
pinacolada    834
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
BitMaster    8651
Edit: Correction after some more memories surfacing: You need to know the numbers of parameters. However, in practically all cases this is not a problem, like with printf & co: The number of parameters and their type are implicity passed through the format string. I used ellipses once or twice for other tasks in the past and in all cases this problem solved itself.

[edited by - BitMaster on January 18, 2004 6:40:20 PM]

Share this post


Link to post
Share on other sites