Portable to pass va_list as va_arg parameter?

Discussion in 'C Programming' started by skillzero@gmail.com, Feb 20, 2007.

  1. Guest

    Is there a portable way to pass a va_list as a parameter to another
    function taking a variable argument list?

    I have a function that takes a printf-like format string and I'd like
    to use something like %V to pass in another format string and a
    va_list to allow nesting. It happens to work on my compiler, but I
    wasn't sure if it's portable to use va_list's as parameters to a
    variable argument function because va_list isn't always just a simple
    pointer.

    For example, if MyPrintF was like printf, but supported %V to take a
    nested format string and va_list:

    void PrintLabelF( const char *format, ... )
    {
    va_list args;

    va_start( args, format );
    MyPrintF( "Label: %V\n", format, args );
    va_end( args );
    }

    // Print "Label: test 123\n"
    PrintLabelF( "test %d", 123 );

    Is it portable to do the following to get the nested va_list?

    const char * nestedFormat;
    va_list nestedArgs;

    nestedFormat = va_arg( args, const char * );
    nestedArgs = va_arg( args, va_list );
    vprintf( nestedFormat, nestedArgs );
    , Feb 20, 2007
    #1
    1. Advertising

  2. wrote:
    > Is there a portable way to pass a va_list as a parameter to another
    > function taking a variable argument list?
    >
    > I have a function that takes a printf-like format string and I'd like
    > to use something like %V to pass in another format string and a
    > va_list to allow nesting. It happens to work on my compiler, but I
    > wasn't sure if it's portable to use va_list's as parameters to a
    > variable argument function because va_list isn't always just a simple
    > pointer.
    >
    > For example, if MyPrintF was like printf, but supported %V to take a
    > nested format string and va_list:
    >
    > void PrintLabelF( const char *format, ... )
    > {
    > va_list args;
    >
    > va_start( args, format );
    > MyPrintF( "Label: %V\n", format, args );
    > va_end( args );
    > }
    >
    > // Print "Label: test 123\n"
    > PrintLabelF( "test %d", 123 );
    >
    > Is it portable to do the following to get the nested va_list?
    >
    > const char * nestedFormat;
    > va_list nestedArgs;
    >
    > nestedFormat = va_arg( args, const char * );
    > nestedArgs = va_arg( args, va_list );
    > vprintf( nestedFormat, nestedArgs );


    No, that's not portable. It can break for multiple reasons, for
    example if va_list is a typedef for short, or if it is a typedef for
    an array type. What you can do, however, is make MyPrintF accept a
    pointer to a va_list.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 20, 2007
    #2
    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. Douwe
    Replies:
    3
    Views:
    734
    Chris Torek
    Aug 30, 2003
  2. Tor Rustad

    Howto pass va_list to another va func

    Tor Rustad, Apr 25, 2007, in forum: C Programming
    Replies:
    4
    Views:
    493
    Tor Rustad
    Apr 25, 2007
  3. Replies:
    21
    Views:
    3,145
    Tim Rentsch
    Feb 12, 2009
  4. Keith
    Replies:
    1
    Views:
    252
    Gabriel Genellina
    Feb 3, 2009
  5. Christopher
    Replies:
    39
    Views:
    1,696
    Robert Miles
    Apr 22, 2012
Loading...

Share This Page