Thread: Function with variable number of arguments

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Function with variable number of 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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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
    Code:
    printf("Hey!");
    all the time, and that gives 0 arguments to the ... part of printf.

    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    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.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by andrewg View Post
    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.
    If you'd walk this way, I have a lovely C++ I'd think you'd be interested in.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    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.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    Well, if I could use C++ it is trivial :-).

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by andrewg View Post
    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.
    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.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by andrewg View Post
    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.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Variable number of arguments in function.
    By kamoda_pawel in forum C Programming
    Replies: 1
    Last Post: 01-18-2007, 07:18 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM