Function with variable number of arguments

Discussion in 'C Programming' started by Nimmi Srivastav, Sep 13, 2005.

  1. Consider two functions A and B, both of which accept a variable number
    of arguments (va_start, va-arg, va_end). Is there an easy way for
    arguments passed to A to be, in turn, passed to B? (For example, if A
    is a wrapper function around B).

    Thanks,
    Nimmi
     
    Nimmi Srivastav, Sep 13, 2005
    #1
    1. Advertising

  2. Groovy hepcat Nimmi Srivastav was jivin' on 12 Sep 2005 20:33:59 -0700
    in comp.lang.c.
    Function with variable number of arguments's a cool scene! Dig it!

    >Consider two functions A and B, both of which accept a variable number
    >of arguments (va_start, va-arg, va_end). Is there an easy way for
    >arguments passed to A to be, in turn, passed to B? (For example, if A
    >is a wrapper function around B).


    No. But what you can do is rewrite B() as two functions; one that
    does all the real work and accepts a va_list paramer, and the other
    just a wrapper around this. You then pass the va_list initialised in
    A() to the va_list version of B(). For example:

    void vB(const char *fmt, va_list arg)
    {
    while(whatever)
    {
    somevar = va_arg(arg, some_type);
    use somevar;
    }
    }

    void B(const char *fmt, ...)
    {
    va_list arg;

    va_start(arg, fmt);
    vB(fmt, arg);
    va_end(arg);
    }

    void A(const char *fmt, ...)
    {
    va_list arg;

    va_start(arg, fmt);
    vB(fmt, arg);
    va_end(arg);
    }

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Sep 13, 2005
    #2
    1. Advertising

  3. Nimmi Srivastav

    grid Guest

    > void vB(const char *fmt, va_list arg)
    > {
    > while(whatever)
    > {
    > somevar = va_arg(arg, some_type);
    > use somevar;
    > }
    > }

    In this context,this is what the standard has to say :
    If access to the varying arguments is desired, the called function shall
    declare an object (referred to as ap in this subclause)having type
    va_list. The object ap may be passed as an argument to another function;
    if that function invokes the va_arg macro with parameter ap, the value
    of ap in the calling function is indeterminate and shall be passed to
    the va_end macro prior to any further reference to ap.

    Further ,It is permitted to create a pointer to a va_list and pass that
    pointer to another function, in which case the original function may
    make further use of the original list after the other function returns.

    >
    > void B(const char *fmt, ...)
    > {
    > va_list arg;
    >
    > va_start(arg, fmt);
    > vB(fmt, arg);
    > va_end(arg);
    > }
    >


    So is the above code valid.I am not sure I completely understand what
    the standard says.

    TIA
     
    grid, Sep 19, 2005
    #3
  4. Groovy hepcat grid was jivin' on Mon, 19 Sep 2005 15:10:13 +0530 in
    comp.lang.c.
    Re: Function with variable number of arguments's a cool scene! Dig it!

    >> void vB(const char *fmt, va_list arg)
    >> {
    >> while(whatever)
    >> {
    >> somevar = va_arg(arg, some_type);
    >> use somevar;
    >> }
    >> }

    >In this context,this is what the standard has to say :
    >If access to the varying arguments is desired, the called function shall
    >declare an object (referred to as ap in this subclause)having type
    >va_list. The object ap may be passed as an argument to another function;
    >if that function invokes the va_arg macro with parameter ap, the value
    >of ap in the calling function is indeterminate and shall be passed to
    >the va_end macro prior to any further reference to ap.


    That's exactly what I've done here. The B() function (below)
    initialises a va_list (which I've called arg, not ap) then passes it
    to the vB() function (above). This then processes the arguments by
    using the va_arg macro.
    I should probably point out (for the less clueful people reading
    this) that vB() is incomplete, of course. It's mostly psuedocode. But
    the use of va_arg is valid as long as some_type is a valid type name
    and somevar an object of that type and the type is the same as the
    coresponding passed argument (after promotion).

    >Further ,It is permitted to create a pointer to a va_list and pass that
    >pointer to another function, in which case the original function may
    >make further use of the original list after the other function returns.


    Right. But there's no need to do that here.

    >> void B(const char *fmt, ...)
    >> {
    >> va_list arg;
    >>
    >> va_start(arg, fmt);
    >> vB(fmt, arg);
    >> va_end(arg);
    >> }
    >>

    >
    >So is the above code valid.I am not sure I completely understand what
    >the standard says.


    Yes (notwithstanding the psuedocode in the vB() function). See
    explanation above.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Sep 20, 2005
    #4
    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. Alex
    Replies:
    1
    Views:
    373
    Ron Natalie
    Oct 16, 2003
  2. Vince
    Replies:
    3
    Views:
    384
    Victor Bazarov
    Nov 10, 2004
  3. jmborr
    Replies:
    1
    Views:
    427
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    836
  5. Navaneeth
    Replies:
    4
    Views:
    554
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page