Is it possible to define the function which take no or one parameter? All examples I see have at least one argument and the description claims that this is required by va_<> macros to establish the pointer to the list or arguments.
Is it possible to define the function which take no or one parameter? All examples I see have at least one argument and the description claims that this is required by va_<> macros to establish the pointer to the list or arguments.
In C, there's no way (that I know of, and I'm pretty sure no way at all) for ... to limit the number of arguments that comes in. Now ... doesn't have to have any arguments that correspond to it, if that's what you're asking -- people do
all the time, and that gives 0 arguments to the ... part of printf.Code:printf("Hey!");
EDIT: Important part I forgot: there's also no mechanism for ... to tell you how many arguments were passed in. In the printf case, because there are no format specifiers in the conversion string it doesn't try to get anything out of the va_list. If you don't have some other way to tell whether there's something there or not, you could be in for some pain.
Last edited by tabstop; 12-18-2010 at 12:43 PM.
There is no way to guarantee that there is exactly zero or one parameter. As tabstop pointed out, you can use the ..., but you need some mechanism to know how many optional parameters you expect. In printf's case, it counts the number of % specifiers in the fmt string.
Perhaps if you tell us what you're ultimately trying to achieve, we can help you find an alternative.
Well, all I see from the examples, including print, is that there is always at least on argument of known type.
What I need is the ability to make a calls like this:
function()
or
function(int)
to provide backward compatibility.
I know that (1) there will be no or one argument, (2) if the argument is present it is of int type.
I see that the compiler has no problem with the function defined as f(...). What I do not know is what to put in the body of f() to handle both scenarios.
Well, if I could use C++ it is trivial :-).
Unless you use old varargs Old Varargs - The GNU C Library
Edit : On second thought, you still won't know how many arguments are passed ...
Last edited by Bayint Naung; 12-18-2010 at 01:43 PM.
Unfortunately, you can't overload a function in C. The compiler will happily accept your f(...), but without a fixed parameter, the va_* macros don't know how to find the first optional argument, or how many there are. These macros are just ways of bumping a pointer on down the stack and it doesn't know if the data in there is good or bogus.
Generally, when you need to keep an old function for backwards compatability, and are providing a new version of the function with a different signature, you give it a new name, like foo2, especially if there are other people who have been or will be using your API. If you have complete control of any code using your API however, here are some possible work-arounds:
Since you have a very explicit case of exactly zero or 1 int arguments, my preference would be to make the function always take one argument and use a sentinel value like 0 or -1 if possible to signify "no argument".
If that's not an option (i.e. the function needs to work for every possible int value in the argument), you can make the function take two arguments, the first argument saying whether to use or ignore the second argument.
You could have a variadic function with one argument before the ... to tell you whether to expect a second argument, but I don't think it's a very good idea. First, neither you nor the compiler can guarantee that the caller passed anything or that it's the right type, and second, it's just extra code to maintain.
You may be able to conquor this by piggybacking on an existing #define someplace. You would need a parameter that would tell you the version of the code or perhaps the version of the OS, or some other feature that is relevent.
For example:
Code:#ifdef WIN_32 int foo(int x) { return x *2; } #else void foo(void) { x = x * 2; } #endif