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 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)
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)
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
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)
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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));
...
|
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...
|
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...
|
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...
|
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...
|
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>
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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: 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...
| |