Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Function with variable number of arguments (http://www.velocityreviews.com/forums/t439376-function-with-variable-number-of-arguments.html)

Nimmi Srivastav 09-13-2005 03:33 AM

Function with variable number of arguments
 
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


Peter Shaggy Haywood 09-13-2005 06:17 AM

Re: Function with variable number of arguments
 
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"?

grid 09-19-2005 09:40 AM

Re: Function with variable number of arguments
 
> 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

Peter Shaggy Haywood 09-20-2005 02:10 AM

Re: Function with variable number of arguments
 
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!

>> 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.


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.

>> 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.


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"?


All times are GMT. The time now is 05:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.