473,699 Members | 2,302 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 2792
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
4963
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
14939
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
6289
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
7674
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
2294
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
4348
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
4336
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
4317
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
3271
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
4652
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
8689
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...
0
8618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
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
7752
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
5875
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
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.