473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Portable to pass va_list as va_arg parameter?

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
1 9419
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rich Herrick | last post by:
Is it defined behavior to use a reference to a va_list to allow calling a function to extract arguments from the argument list, then upon returning to the calling function, continue extracting...
5
by: John Guo | last post by:
Hi all, I am trying to nest two functions both with variable length arguments. (The only reason I nested them is that I called the outer function in too many places, I don't want to replace...
2
by: Andrej Prsa | last post by:
Hi! If I declare two functions like this: int execute_command (char *name, ...) { va_list args; va_start (args, name); my_func (1, args);
11
by: thierrydollar | last post by:
Hi, I have written a very simple program using variable arguments calls and I get strange things that I cannot explain. I have one function (add) that displays two parameters. It works well...
2
by: A. Farber | last post by:
Hello, I'm programming a web application and for that I'm trying to extend the standard C string functions, so that they can handle the application/x-www-form-urlencoded format (which encodes...
3
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
1
by: pete m | last post by:
I would like to support stdio functions for an extremely primitive type system, as shown in the attached sample program, using dynamic creation of a va_list. I've tested it on successfully a...
2
by: Peter Nilsson | last post by:
Jesse Ziser <d...@spam.diewrote: The detail in VARIABLE ARGUMENTS explicitly allows it. Nothing prohibits it. Only va_start and va_end must 'be invoked in the function accepting varying number...
6
by: Laurent Deniau | last post by:
When I compile the code below with gcc -std=c99 -W -Wall -pedantic -O3 -Winline, it reports the following: variadic.c: In function ‘fv’: variadic.c:12: warning: function ‘fv’ can never be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.