How to pass varargs arguments to 2nd function?

Discussion in 'C Programming' started by antianti@rocketmail.com, Jul 27, 2008.

  1. Guest

    Hello,

    I have a function that has a variable # of arguments of varying types.

    int foo(short a, ...)
    {
    va_list argp
    va_start(argp, a);
    ...
    va_end(argp);
    }

    I want to be able to call another function, offering the same
    variable list of arguments to it.

    int bar(int a, ...)
    {
    va_list argp
    va_start(short, a);
    ...
    va_end(argp);
    }

    How could such a thing be done?

    The function bar does not have to use the ... expression
    if not doing so will help.

    Thanks.
     
    , Jul 27, 2008
    #1
    1. Advertising

  2. On Sun, 27 Jul 2008 13:58:00 -0700, antianti wrote:
    > Hello,
    >
    > I have a function that has a variable # of arguments of varying types.
    >
    > int foo(short a, ...)
    > {
    > va_list argp
    > va_start(argp, a);
    > ...
    > va_end(argp);
    > }
    >
    > I want to be able to call another function, offering the same variable
    > list of arguments to it.
    >
    > How could such a thing be done?
    >
    > The function bar does not have to use the ... expression if not doing so
    > will help.


    You can make bar accept a va_list argument, and have bar use va_arg on
    foo's list.

    int bar(va_list argp)
    {
    /* ... */
    }

    int foo(short a, ...)
    {
    va_list argp;
    va_start(argp, a);
    bar(argp);
    va_end(argp);
    }

    Consider taking a look at the vprintf (and related) library functions.
    They are specifically there to be called this way as well.
     
    Harald van Dijk, Jul 27, 2008
    #2
    1. Advertising

  3. On Jul 27, 4:58 pm, wrote:
    > Hello,
    >
    > I have a function that has a variable # of arguments of varying types.
    >
    > int foo(short a, ...)
    > {
    >    va_list argp
    >    va_start(argp, a);
    >  ...
    >    va_end(argp);
    >
    > }
    >
    > I want to be able to call another function, offering the same
    > variable list of arguments  to it.
    >
    > int bar(int a, ...)
    > {
    >    va_list argp
    >    va_start(short, a);
    >  ...
    >    va_end(argp);
    >
    > }
    >
    > How could such a thing be done?
    >
    > The function bar does not have to use the ... expression
    > if not doing so will help.
    >
    > Thanks.


    <http://c-faq.com/> Question 15.12: <http://c-faq.com/varargs/
    handoff.html>

    --
    Robert Gamble
     
    Robert Gamble, Jul 27, 2008
    #3
  4. Guest

    I tried precisely what was in the FAQ and it didn't work with GNU CC.

    Ah well, there is a solution: Pass a pointer to the location in the
    stack where the varargs are located.
     
    , Jul 28, 2008
    #4
  5. In article <>,
    <> wrote:
    >
    >I tried precisely what was in the FAQ and it didn't work with GNU CC.
    >
    >Ah well, there is a solution: Pass a pointer to the location in the
    >stack where the varargs are located.
    >


    Beavis&Butthead: ork, ork, ork. He said "stack"...
     
    Kenny McCormack, Jul 28, 2008
    #5
  6. writes:

    > I tried precisely what was in the FAQ and it didn't work with GNU
    > CC.


    It does for me. Can you post an example of it not working? It is
    possible there is a misunderstanding.

    > Ah well, there is a solution: Pass a pointer to the location in the
    > stack where the varargs are located.


    Yuck. As a famous pig once said: "You're skate'n on thin ice, frog!".

    --
    Ben.
     
    Ben Bacarisse, Jul 28, 2008
    #6
    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. Ian Partridge

    passing varargs to another function

    Ian Partridge, Dec 24, 2003, in forum: C Programming
    Replies:
    2
    Views:
    782
    Derk Gwen
    Dec 24, 2003
  2. Sandra-24
    Replies:
    1
    Views:
    454
    Sandra-24
    Dec 22, 2006
  3. jmborr
    Replies:
    1
    Views:
    435
    Stargaming
    Nov 3, 2007
  4. David G

    varargs function, is this legal?

    David G, Oct 16, 2009, in forum: C Programming
    Replies:
    4
    Views:
    451
    David G
    Oct 16, 2009
  5. Daniel Berger
    Replies:
    4
    Views:
    132
    Patrick Hurley
    Mar 18, 2007
Loading...

Share This Page