Nesting variadic functions

Discussion in 'C Programming' started by Boltar, Sep 17, 2007.

  1. Boltar

    Boltar Guest

    Hi

    Is it possible to nest variadic functions? Eg to do something like
    this:

    void mainfunc(char *fmt, ...)
    {
    va_list args;
    va_start(args,fmt);

    do something;

    subfunc(fmt, args);
    va_end(args);
    }



    void subfunc(char *fmt, ...)
    {
    va_list args;

    va_start(args,fmt);
    etc
    va_end(args);
    }


    The above type of code will compile but the arguments in subfunc() are
    corrupted. Is there a way to do this?

    Thanks for any help

    B2003
    Boltar, Sep 17, 2007
    #1
    1. Advertising

  2. Boltar said:

    > Hi
    >
    > Is it possible to nest variadic functions? Eg to do something like
    > this:
    >
    > void mainfunc(char *fmt, ...)
    > {
    > va_list args;
    > va_start(args,fmt);
    >
    > do something;
    >
    > subfunc(fmt, args);
    > va_end(args);
    > }
    >
    >
    >
    > void subfunc(char *fmt, ...)


    Oh, and you were doing so well.

    void subfunc(char *fmt, va_list args)

    > {
    > va_list args;


    Drop this...

    >
    > va_start(args,fmt);


    ....and this...

    > etc
    > va_end(args);


    ....and this, and let the caller's va_end take the strain. (In the caller,
    don't call va_args after calling subfunc - or, if you must, you'll first
    have to call va_end and va_start again.)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 17, 2007
    #2
    1. Advertising

  3. Boltar

    Boltar Guest

    On Sep 17, 1:20 pm, Richard Heathfield <> wrote:
    > Boltar said:
    >
    >
    >
    >
    >
    > > Hi

    >
    > > Is it possible to nest variadic functions? Eg to do something like
    > > this:

    >
    > > void mainfunc(char *fmt, ...)
    > > {
    > > va_list args;
    > > va_start(args,fmt);

    >
    > > do something;

    >
    > > subfunc(fmt, args);
    > > va_end(args);
    > > }

    >
    > > void subfunc(char *fmt, ...)

    >
    > Oh, and you were doing so well.
    >
    > void subfunc(char *fmt, va_list args)
    >
    > > {
    > > va_list args;

    >
    > Drop this...
    >
    >
    >
    > > va_start(args,fmt);

    >
    > ...and this...
    >
    > > etc
    > > va_end(args);

    >
    > ...and this, and let the caller's va_end take the strain. (In the caller,
    > don't call va_args after calling subfunc - or, if you must, you'll first
    > have to call va_end and va_start again.)


    Works nicely. Thanks.

    B2003
    Boltar, Sep 17, 2007
    #3
  4. In article <>,
    Boltar <> wrote:

    >Is it possible to nest variadic functions? Eg to do something like
    >this:


    Unfortunately C doesn't have the equivalent of Lisp's "apply".

    Look at the printf() family of functions. In particular observe how
    for each function there is a "v" version, that takes a va_list instead
    of multiple arguments. You may find it useful to do the same for all
    your variadic functions - not just the internal ones - to make it
    convenient for others to write wrappers around them.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Sep 17, 2007
    #4
  5. Boltar

    CBFalconer Guest

    Boltar wrote:
    >
    > Is it possible to nest variadic functions? Eg to do something like
    > this:
    >
    > void mainfunc(char *fmt, ...) {
    > va_list args;
    >
    > va_start(args,fmt);
    > do something;
    > subfunc(fmt, args);
    > va_end(args);
    > }
    >
    > void subfunc(char *fmt, ...) {
    > va_list args;
    >
    > va_start(args,fmt);
    > etc
    > va_end(args);
    > }
    >
    > The above type of code will compile but the arguments in subfunc()
    > are corrupted. Is there a way to do this?


    Define the parameter as "const char *fmt". Maybe 'do something' or
    subfunct() is altering *fmt? (or fmt). It looks like O(n * n)
    where n is the number of arguments.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 17, 2007
    #5
    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. Colin Walters
    Replies:
    2
    Views:
    502
    Ben Pfaff
    Feb 13, 2004
  2. Ross A. Finlayson
    Replies:
    19
    Views:
    568
    Keith Thompson
    Mar 10, 2005
  3. Replies:
    2
    Views:
    327
    Dave Thompson
    Feb 27, 2006
  4. Replies:
    5
    Views:
    346
  5. Trans
    Replies:
    10
    Views:
    279
    Sean O'Halpin
    Sep 16, 2005
Loading...

Share This Page