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

va_list help

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.