473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Nov 15 '05 #1
3 2761
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"?
Nov 15 '05 #2
> 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
Nov 15 '05 #3
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"?
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
3
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
5
by: kris | last post by:
Hi I have written a program which prints the hostid on a linux system. The programm uses gethostid() function call which is defined in the library file unistd.h. But my programm gets compiled...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.