Thread: Optional Function Parameters after ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    24

    Optional Function Parameters after ...

    Is this possible? I thought I had seen this sort of thing somewhere, but I can't find it or replicate it. This is my attempt:

    Code:
    void example(int notimportant, ..., int terminator = 0)
    Thought that would be a really cool way to handle variable arguments... That way, you wouldn't have to supply NULL or 0 at the end.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    Look into stdarg.h - va_arg() va_start(), etc.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - The elipses has to be the final argument in the function.
    2 - There are no default values in C. (The = 0 is invalid.)
    3 - You don't have to have NULL at the end. You don't append a null when using printf do you?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    24
    printf() uses a format string to get its arguments... I was just trying to get rid of any need for that.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right, but it still doesn't have the argument list end in a NULL as you describe. Furthermore, you can pass NULL pointers to printf and have it handle them. You wouldn't be able to do this in your example, because that would signify the end of the arguments, when there would actually be more.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by LPP
    printf() uses a format string to get its arguments... I was just trying to get rid of any need for that.
    Have you read the manual on va_arg?
    printf uses format cause the optional argument lst is flexible.

    In your case - you know exacly what you get, if you get something

    So in your function you should initialize variables representing optional parameters with default values, then process the va_arg array to reinitialize the part of them with the values supplied, then do your stuff
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    From Linux manpages:

    EXAMPLE
    The function foo takes a string of format characters and prints out the
    argument associated with each format character based on the type.
    Code:
     
                 #include <stdio.h>
                  #include <stdarg.h>
    
                  void foo(char *fmt, ...) {
                       va_list ap;
                       int d;
                       char c, *s;
    
                       va_start(ap, fmt);
                       while (*fmt)
                            switch(*fmt++) {
                            case ’s’:           /* string */
                                 s = va_arg(ap, char *);
                                 printf("string %s\n", s);
                                 break;
                            case ’d’:           /* int */
                                 d = va_arg(ap, int);
                                 printf("int %d\n", d);
                                 break;
                             case ’c’:           /* char */
                                 /* need a cast here since va_arg only
                                    takes fully promoted types */
                                 c = (char) va_arg(ap, int);
                                 printf("char %c\n", c);
                                 break;
                            }
                       va_end(ap);
                  }
    Also notice the variable arguments are the last paramether of the function so your declaration is invalid. It must end with "...". So
    Code:
    void foo(int a, ...,int b)
    is invalid you must declare
    Code:
    void foo(int a, int b, ...)

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf() uses a format string to get its arguments... I was just trying to get rid of any need for that.
    And exec functions use a NULL pointer to mark the end of the list.

    Or you could do
    int adder ( int numvals, ... );

    And do
    result = adder ( 5, 1, 1, 1, 2, 2 );

    Somehow, you need to count the number of args in a variadic function.
    - encode the number in a format string, like printf/scanf
    - mark the end of the list with some special value, like exec functions do with NULL
    - explicitly state the number of parameters, like the adder function above.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM