Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function with variable number of arguments

Reply
Thread Tools

Function with variable number of arguments

 
 
Nimmi Srivastav
Guest
Posts: n/a
 
      09-13-2005
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

 
Reply With Quote
 
 
 
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      09-13-2005
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"?
 
Reply With Quote
 
 
 
 
grid
Guest
Posts: n/a
 
      09-19-2005
> 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
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      09-20-2005
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"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function that expects variable arguments from a functionwith variable arguments Navaneeth C Programming 4 11-20-2010 05:35 AM
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
Variable number of arguments when calling a DLL function Vince C++ 3 11-10-2004 05:57 PM
variable number of arguments + function pointers Alex C++ 1 10-16-2003 10:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57