getting and passing variable number of parameters

Discussion in 'C Programming' started by Michael Cohen, Dec 25, 2003.

  1. Hi,
    I have to write a function that gets variable number of parameters and
    pass them to another function (existing).
    The problem is that the prototype of the function I call has ",..." so
    I can't just pass it parameters in the format of va_list.

    The following code does not work since f2 doesn't take arg_list (the
    prototype of f2 is the same as f1's)

    void f1(char a ,...)
    va_list arg_list;
    va_start (arg_list, a);
    f2(a,arg_list); <==== f2 doesn't take arg_list
    va_end;

    any ideas how I can do it?
    Thanks,
    Michael
    Michael Cohen, Dec 25, 2003
    #1
    1. Advertising

  2. Michael Cohen

    Jack Klein Guest

    On 25 Dec 2003 03:32:30 -0800, (Michael Cohen)
    wrote in comp.lang.c:

    > Hi,
    > I have to write a function that gets variable number of parameters and
    > pass them to another function (existing).
    > The problem is that the prototype of the function I call has ",..." so
    > I can't just pass it parameters in the format of va_list.
    >
    > The following code does not work since f2 doesn't take arg_list (the
    > prototype of f2 is the same as f1's)
    >
    > void f1(char a ,...)
    > va_list arg_list;
    > va_start (arg_list, a);
    > f2(a,arg_list); <==== f2 doesn't take arg_list
    > va_end;
    >
    > any ideas how I can do it?
    > Thanks,
    > Michael
    >


    There is no way to do this using standard C. You will need to consult
    a compiler-specific group to see if you can hack up some compiler
    specific solution.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Dec 25, 2003
    #2
    1. Advertising

  3. (Michael Cohen) wrote...

    > Hi,
    > I have to write a function that gets variable number of parameters and
    > pass them to another function (existing).
    > The problem is that the prototype of the function I call has ",..." so
    > I can't just pass it parameters in the format of va_list.


    What you are trying to do is not possible using standard C. You have
    two options, one of which has already been suggested. The other is
    this:

    If you have source access to the function accepting variadic arguments
    that you would like to call, renaming it and modifying it's prototype
    may be a better idea. For example:

    void mylog(const char *fmt, ...)
    {
    va_list ap;
    va_start(ap, fmt);
    /* ... code ... */
    va_end(ap);
    }


    To make this function callable with variable arguments at runtime from
    other functions, you could do something like:

    void vmylog(const char *fmt, va_list ap)
    {
    /* ... code ... */
    }

    void mylog(const char *fmt, ...)
    {
    va_list ap;
    va_start(ap);
    vmylog(fmt, ap);
    va_end(ap);
    }


    You can now simply pass vmylog a va_list when you please. If you have
    not got source access to the procedure you wish to call, then you have
    to go non-portable, otherwise you are screwed.


    David.
    David M. Wilson, Dec 26, 2003
    #3
    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. Mike Dinnis
    Replies:
    4
    Views:
    509
    Erik J Sawyer
    Nov 11, 2003
  2. Paul D. Fox
    Replies:
    1
    Views:
    14,201
  3. david
    Replies:
    6
    Views:
    411
    Tim H
    Feb 14, 2008
  4. David Portabella
    Replies:
    0
    Views:
    370
    David Portabella
    Jun 13, 2008
  5. eran
    Replies:
    7
    Views:
    395
Loading...

Share This Page