By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,827 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,827 IT Pros & Developers. It's quick & easy.

Portable to pass va_list as va_arg parameter?

P: n/a
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 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
sk*******@gmail.com 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.

Feb 20 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.