Function with variable number of arguments

N

Nimmi Srivastav

Consider two functions A and B, both of which accept a variable number
of arguments (va_start, va-arg, va_end). Is there an easy way for
arguments passed to A to be, in turn, passed to B? (For example, if A
is a wrapper function around B).

Thanks,
Nimmi
 
P

Peter Shaggy Haywood

Groovy hepcat Nimmi Srivastav was jivin' on 12 Sep 2005 20:33:59 -0700
in comp.lang.c.
Function with variable number of arguments's a cool scene! Dig it!
Consider two functions A and B, both of which accept a variable number
of arguments (va_start, va-arg, va_end). Is there an easy way for
arguments passed to A to be, in turn, passed to B? (For example, if A
is a wrapper function around B).

No. But what you can do is rewrite B() as two functions; one that
does all the real work and accepts a va_list paramer, and the other
just a wrapper around this. You then pass the va_list initialised in
A() to the va_list version of B(). For example:

void vB(const char *fmt, va_list arg)
{
while(whatever)
{
somevar = va_arg(arg, some_type);
use somevar;
}
}

void B(const char *fmt, ...)
{
va_list arg;

va_start(arg, fmt);
vB(fmt, arg);
va_end(arg);
}

void A(const char *fmt, ...)
{
va_list arg;

va_start(arg, fmt);
vB(fmt, arg);
va_end(arg);
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
G

grid

void vB(const char *fmt, va_list arg)
{
while(whatever)
{
somevar = va_arg(arg, some_type);
use somevar;
}
}
In this context,this is what the standard has to say :
If access to the varying arguments is desired, the called function shall
declare an object (referred to as ap in this subclause)having type
va_list. The object ap may be passed as an argument to another function;
if that function invokes the va_arg macro with parameter ap, the value
of ap in the calling function is indeterminate and shall be passed to
the va_end macro prior to any further reference to ap.

Further ,It is permitted to create a pointer to a va_list and pass that
pointer to another function, in which case the original function may
make further use of the original list after the other function returns.
void B(const char *fmt, ...)
{
va_list arg;

va_start(arg, fmt);
vB(fmt, arg);
va_end(arg);
}

So is the above code valid.I am not sure I completely understand what
the standard says.

TIA
 
P

Peter Shaggy Haywood

Groovy hepcat grid was jivin' on Mon, 19 Sep 2005 15:10:13 +0530 in
comp.lang.c.
Re: Function with variable number of arguments's a cool scene! Dig it!
In this context,this is what the standard has to say :
If access to the varying arguments is desired, the called function shall
declare an object (referred to as ap in this subclause)having type
va_list. The object ap may be passed as an argument to another function;
if that function invokes the va_arg macro with parameter ap, the value
of ap in the calling function is indeterminate and shall be passed to
the va_end macro prior to any further reference to ap.

That's exactly what I've done here. The B() function (below)
initialises a va_list (which I've called arg, not ap) then passes it
to the vB() function (above). This then processes the arguments by
using the va_arg macro.
I should probably point out (for the less clueful people reading
this) that vB() is incomplete, of course. It's mostly psuedocode. But
the use of va_arg is valid as long as some_type is a valid type name
and somevar an object of that type and the type is the same as the
coresponding passed argument (after promotion).
Further ,It is permitted to create a pointer to a va_list and pass that
pointer to another function, in which case the original function may
make further use of the original list after the other function returns.

Right. But there's no need to do that here.
So is the above code valid.I am not sure I completely understand what
the standard says.

Yes (notwithstanding the psuedocode in the vB() function). See
explanation above.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top