473,748 Members | 8,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2797
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 "technicall y correct" English; but since when was rock & roll "technicall y 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)havin g 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)havin g 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 (notwithstandin g 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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 15 '05 #4

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

Similar topics

9
4964
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
3
14945
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
7
6295
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 passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
2
7677
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this behavior? <!-- main page --> <html> <head> <script type="text/javascript">
10
2301
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 values. Works in both ie and firefox. Parameters styleType = top | left | width | height toNumber = the new value of the style then you pass in as many ids as you would like.
18
4354
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
3
4340
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 that function (using its pointer) and call it with the same argument list that was passed into the static method.
28
4333
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
3274
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 function are of type int. (My question has nothing to do with the definition of the function foo, so don't bother about it.) If I call the function as: foo(2,3,4,5,6,7,8);/*More arguments than expected*/
5
4658
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 without any warnings even if I didnot include any of the header files. can I know how does this happen i.e how does the compiler identifies this function gethostid. Is there any default path from where the compiler picks up the definition from.
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.