473,405 Members | 2,272 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,405 software developers and data experts.

va_list help

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 every calls with the new one).

void outerFunction(const int arg1, const int arg2, ...)
{
int arg3;
va_list ap;
va_start(ap, arg2);
innerFunction(arg3, ap);
va_end(ap);
}
void innerFunction(int arg, ...)
{
va_list ap;
va_start(ap, arg);
char a[255];
strcpy(a, va_arg(ap,char*));
int x = va_arg(ap,int);
cout<<a<<endl;
cout<<x<<endl;
va_end(ap);
}

Say if I call outerfunction with

outFunction(1,2, "ProcessName", 5)

As I debug this, it seems that ap before passed in is ok to read using
va_arg(), but in innerfunction, it doesn't work, read out some garbage
in a and x.

Could someone give some comments on this? Any help is greatly
appreciated.

John

Jul 23 '05 #1
5 2022
John Guo wrote:
void innerFunction(int arg, ...)


Make that:

void innerFunction(int arg, va_list ap)

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2
That's similar to the prototype of vsprintf (i.e., take arguent va_list
ap).

If I do that, can the call to innerfunction directly still work?
e.g. innerFunction(1, 'sldafkdlasf", 2)

Jul 23 '05 #3
John Guo wrote:
I am trying to nest two functions both with variable length arguments.
That's generally impossible.
(The only reason I nested them is that I called the outer function in
too many places, I don't want to replace every calls with the new
one).

void outerFunction(const int arg1, const int arg2, ...)
{
int arg3;
va_list ap;
va_start(ap, arg2);
innerFunction(arg3, ap);
va_end(ap);
}
void innerFunction(int arg, ...)
{
va_list ap;
va_start(ap, arg);
char a[255];
strcpy(a, va_arg(ap,char*));
int x = va_arg(ap,int);
cout<<a<<endl;
cout<<x<<endl;
va_end(ap);
}

Say if I call outerfunction with

outFunction(1,2, "ProcessName", 5)

As I debug this, it seems that ap before passed in is ok to read using
va_arg(), but in innerfunction, it doesn't work, read out some garbage
in a and x.

Could someone give some comments on this? Any help is greatly
appreciated.


If you need some common code called from both functions, you will need
to defined a function that takes va_args (take a look at vfprintf in
the source for your standard library, or on the web somewhere). Make
both your functions that have ellipsis call that function (like the
'fprintf' does with 'vfprintf')

Good luck!

V
Jul 23 '05 #4
John Guo wrote:
That's similar to the prototype of vsprintf (i.e., take arguent va_list
ap).
Yup. Same problem, same solution.

If I do that, can the call to innerfunction directly still work?
e.g. innerFunction(1, 'sldafkdlasf", 2)


No. innerFunction now takes two arguments, one of type int and one of
type va_list.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #5
Pete Becker wrote:

No. innerFunction now takes two arguments, one of type int and one of
type va_list.


I should add: often the best solution to splitting a function is not to
make two functions, but three.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter | last post by:
-------------------------------------------------------------------------------- Hi, I was wondering if we can create a va_list by adding objects in the va_list instead of passing in the...
3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
7
by: Jon | last post by:
Is it possible to modify an argument of a va_list and then pass the modified va_list to other functions? void c_fun1(int, va_list args) { int *firstIntPointer = &(va_arg(args, int)); ...
1
by: skillzero | last post by:
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...
8
by: Fred | last post by:
I've got following program encapsuled fscanf, however, it doesn't work. I'm sure that the content format in "a.txt" is OK, the content would be correctly read if using fscanf directly, what's...
5
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote: The relevant paragraph from the Standard is: ---- Begin Quote ---- The type declared is va_list which is an object type suitable for...
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...
10
by: raphfrk | last post by:
Is there any way to pass a variable list to a function, so as to act as a passthrough? for example: void new_printf ( char *buf , ... ) { va_list vlist; <modify buf slightly>
1
by: Chuck Chopp | last post by:
I have some code that is being built on the following: Windows Server 2003, both 32-bit & 64-bit editions Windows Vista, both 32-bit & 64-bit editions Windows Server 2008, both 32-bit & 64-bit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.