Portable to pass va_list as va_arg parameter?

S

skillzero

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 );
 
G

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 );

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top