Variable number of parameters in a function call?

Discussion in 'C Programming' started by Felix Kater, Oct 9, 2004.

  1. Felix Kater

    Felix Kater Guest

    Hi,

    is it possible to define a function in a way that when calling it I can
    insert as many arguments as I want? (I would neither like to define a
    function with 50 default arguments nor prepare a pointer array each time
    before calling the function and pass the array, though.)

    I imagine function calls like this:

    f(a,b,c);
    f(d,e,f,g,h);
    f(a,
    f(b,
    c,
    f(g,h,i),
    e,
    g,
    i,
    c);

    Felix
    Felix Kater, Oct 9, 2004
    #1
    1. Advertising

  2. Felix Kater

    Malcolm Guest

    "Felix Kater" <> wrote
    >
    > is it possible to define a function in a way that when calling it I can
    > insert as many arguments as I want?


    void foo(int N, ...)

    Look up stdarg in your documentation to see how to access the arguments

    You must have some way of telling the function how many arguments you have
    passed, which means the first argument must be of fixed type.
    Malcolm, Oct 9, 2004
    #2
    1. Advertising

  3. > "Felix Kater" <> wrote

    >>f(a,b,c);
    >>f(d,e,f,g,h);
    >>f(a,
    > >f(b,
    >> c,
    >> f(g,h,i),
    >>e,
    > >g,
    > > i,
    >>c);


    > >
    > > is it possible to define a function in a way that when calling it I can
    > > insert as many arguments as I want?


    > "Malcolm wrote:
    > void foo(int N, ...)
    >
    > Look up stdarg in your documentation to see how to access the arguments
    >
    > You must have some way of telling the function how many arguments you have
    > passed, which means the first argument must be of fixed type.
    >


    Is it possible that you're asking this question not knowing the nuts and
    bolts of recursion? MPJ
    Merrill & Michele, Oct 9, 2004
    #3
  4. Felix Kater

    Artie Gold Guest

    Merrill & Michele wrote:
    >>"Felix Kater" <> wrote

    >
    >
    >>>f(a,b,c);
    >>>f(d,e,f,g,h);
    >>>f(a,

    >
    > > >f(b,
    > >> c,
    > >> f(g,h,i),
    > >>e,
    > > >g,
    > > > i,
    > >>c);

    >
    >
    >>>is it possible to define a function in a way that when calling it I can
    >>>insert as many arguments as I want?

    >
    >
    >>"Malcolm wrote:
    >>void foo(int N, ...)
    >>
    >>Look up stdarg in your documentation to see how to access the arguments
    >>
    >>You must have some way of telling the function how many arguments you have
    >>passed, which means the first argument must be of fixed type.
    >>

    >
    >
    > Is it possible that you're asking this question not knowing the nuts and
    > bolts of recursion? MPJ
    >
    >

    I fail to see the relevance of your comment.

    ?

    --ag

    --
    Artie Gold -- Austin, Texas

    "If you don't think it matters, you're not paying attention."
    Artie Gold, Oct 10, 2004
    #4

  5. >> >>"Felix Kater" <> wrote
    > >>>f(a,b,c);
    > >>>f(d,e,f,g,h);
    > >>>f(a,
    > > > >f(b,
    > >>> c,
    > >>> f(g,h,i),
    > >>>e,
    > >> >g,
    > > > > i,
    > > >>c);


    > >>>is it possible to define a function in a way that when calling it I can
    > >>>insert as many arguments as I want?


    > >>"Malcolm wrote:
    > >>void foo(int N, ...)
    > >>
    > >>Look up stdarg in your documentation to see how to access the arguments
    > >>
    > >>You must have some way of telling the function how many arguments you

    have
    > >>passed, which means the first argument must be of fixed type.
    > >>


    > > MPJ wrote:
    > >
    > > Is it possible that you're asking this question not knowing the nuts and
    > > bolts of recursion? MPJ
    > >
    > >


    > "Artie Gold" wrote : I fail to see the relevance of your comment. ?


    The sentence you appended, although followed by a question mark, was a
    comment. The sentence I appended, previous to yours, was a question, as
    properly indicated by the question mark. If Felix the Cat is just a guy who
    knows animated soft porn as opposed to set theory, then this thread can
    gladly continue without my attention. MPJ
    Merrill & Michele, Oct 10, 2004
    #5
  6. Felix Kater

    Malcolm Guest

    "Artie Gold" <> wrote in message
    >>>>f(a,
    >>>>f(b,
    >>>> c,
    >>>> f(g,h,i),
    >>>>e,
    >>>>g,
    >>>> i,
    >>>>c);

    >>
    > > Is it possible that you're asking this question not knowing the nuts and
    > > bolts of recursion? MPJ
    > >
    > >

    > I fail to see the relevance of your comment.
    >

    The OP is calling f() with the return value of f() as an argument. So it is
    possible that what he is really after is recursion, rather than variadic
    functions.
    Malcolm, Oct 10, 2004
    #6
  7. Felix Kater

    Felix Kater Guest

    Merrill & Michele:

    > If Felix the Cat is just a guy who
    > knows animated soft porn as opposed to set theory, then this thread
    > can gladly continue without my attention.


    Hm. Seems to be angry.
    Felix Kater, Oct 10, 2004
    #7
  8. Felix Kater

    Felix Kater Guest

    Merrill & Michele:

    > Is it possible that you're asking this question not knowing the nuts
    > and bolts of recursion?


    No, I am not asking for recursion.

    Felix
    Felix Kater, Oct 10, 2004
    #8
  9. In article <>,
    Felix Kater <> wrote:
    >Merrill & Michele:
    >
    >> Is it possible that you're asking this question not knowing the nuts
    >> and bolts of recursion?

    >
    >No, I am not asking for recursion.
    >
    >Felix


    Then why do you give an example of f(...,f(...),...) ?

    It is certainly possible, of course, that this has nothing to do with the
    stated question (how to use the varargs capability of stdarg.h), but it
    acts as a potential decoy on people's attention.
    Kenny McCormack, Oct 10, 2004
    #9
  10. Felix Kater

    Felix Kater Guest

    Kenny McCormack:

    > it
    > acts as a potential decoy on people's attention.


    I took this example to offer you the possibility to look for solutions
    suitable to my problem. With this I wanted to be sure that the provided
    solution (which I was looking for) could handle nested situations as
    well.

    Felix
    Felix Kater, Oct 11, 2004
    #10
  11. Felix Kater

    John Bode Guest

    Felix Kater <> wrote in message news:<>...
    > Hi,
    >
    > is it possible to define a function in a way that when calling it I can
    > insert as many arguments as I want? (I would neither like to define a
    > function with 50 default arguments nor prepare a pointer array each time
    > before calling the function and pass the array, though.)
    >
    > I imagine function calls like this:
    >
    > f(a,b,c);
    > f(d,e,f,g,h);
    > f(a,
    > f(b,
    > c,
    > f(g,h,i),
    > e,
    > g,
    > i,
    > c);
    >
    > Felix



    Read up on stdarg.h and variadic functions in your favorite C
    reference manual. Note that in Standard C, at least one argument
    *must* be of a fixed type. You must also figure out a way to indicate
    the number and types of arguments being passed.

    Quick, dirty, *UNTESTED* example:

    #include <stdio.h>
    #include <stdarg.h>

    typedef enum {
    EMPTY, // No additional arguments
    RECTYPE_1, // 1 int, 2 double
    RECTYPE_2 // 2 char *, 2 int
    } ArgsType;

    int func(ArgsType t, ...)
    {
    va_list *ap;
    char *a1, *a2;
    int i1, i2;
    double d1, d2;

    int r = 0;

    va_start(ap, t);

    switch(t)
    {
    case EMPTY:
    /* do something interesting */
    break;

    case RECTYPE_1:
    i1 = va_arg(ap, int);
    d1 = va_arg(ap, double);
    d2 = va_arg(ap, dounble);
    /* do something interesting with i1, d1, d2 */
    break;

    case RECTYPE_2:
    a1 = va_arg(ap, char *);
    a2 = va_arg(ap, char *);
    i1 = va_arg(ap, int);
    i2 = va_arg(ap, int);
    /* do something interesting with a1, a2, i1, and i2 */
    break;
    }

    return r; /* r set in interesting bits */
    }

    int main (void)
    {
    func(EMPTY);
    func(RECTYPE_1, 2, 4.0, 8.0);
    func(RECTYPE_2, "foo", "bar", 5, func(RECTYPE_1, 3, 1.0, 0.0));
    return 0;
    }
    John Bode, Oct 11, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Cohen

    getting and passing variable number of parameters

    Michael Cohen, Dec 25, 2003, in forum: C Programming
    Replies:
    2
    Views:
    415
    David M. Wilson
    Dec 26, 2003
  2. Replies:
    5
    Views:
    416
    Earl Purple
    Jan 5, 2006
  3. arne
    Replies:
    16
    Views:
    621
    William Hughes
    Mar 16, 2007
  4. Replies:
    3
    Views:
    822
  5. John Allen
    Replies:
    2
    Views:
    208
    John Allen
    Mar 3, 2009
Loading...

Share This Page