473,382 Members | 1,545 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,382 software developers and data experts.

variable num args via 'va_arg'

'stdarg.h' defines the 'va_arg' type to use in passing variable numbers
of parameters to functions. The example of its use given in the
Dinkumware documentation *seems* to imply that the 'va_arg' list should
*automatically* be terminated with NULL past the user's last argument:
http://www.dinkumware.com/manuals/re...&h=stdarg.html

However, I (for some reason) have to pass in the count of the number of
arguments, or manually pass in NULL as the last argument. Otherwise, the
loop that extracts arguments from the 'va_arg' list runs past the end.

*** Should I *have* to manually pass in NULL or the number of arguments
in order to pass in a variable number of parameters using 'va_arg'?

Below is my test example, with the function call in main():

/************************************************** ****************************
* Demonstrate the use of variable numbers of parameters in a function call.
************************************************** ****************************/
#include <stdarg.h>
#include <iostream>

/************************************************** ****************************
* Declare a struct for use in avg() function.
************************************************** ****************************/
struct Double {
double value;
Double(double v) {
value = v;
}
};

/************************************************** ****************************
* Return the average of all parameters following 'n'.
*
* double avg(Double* x, [Double* x1], [Double* x2], ...)
************************************************** ****************************/
double avg(Double* x, ...) {
// Pass parameters as Double* instead of double or Double& because the
// following types are disallowed:
// * any array type
// * any function type
// * type float (e.g., double)
// * any integer type that changes when promoted
// * a reference type [C++ only]

va_list ap; // declare the parameter list
va_start(ap, x); // start reading the parameter list

double sum = x->value; // initialize sum and count
int count = 1;
while (Double* d = va_arg(ap, Double*)) {
sum += d->value;
count++;
}
va_end(ap); // stop reading the parameter list

return sum/(double)count; // return the average of all the values
}

/************************************************** ****************************
* Main function.
************************************************** ****************************/
int main(int argc, char** argv) {
// NOTE: I should *not* have to pass in NULL at the end, but otherwise
// the loop inside avg() continues 3 places past the end until it
finally
// reads in a NULL.
std::cout << avg(&Double(1.0), &Double(2.0), &Double(3.0),
&Double(4.0), &Double(5.0), &Double(6.0), NULL) <<
"\n";
}

Thanks,
Suzanne

Jul 19 '05 #1
2 7888

"Suzanne Vogel" <su*************@hotmail.com> wrote in message
news:3f**********@news.unc.edu...
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers
of parameters to functions. The example of its use given in the
Dinkumware documentation *seems* to imply that the 'va_arg' list should
*automatically* be terminated with NULL past the user's last argument:
http://www.dinkumware.com/manuals/re...&h=stdarg.html

I guess you could read it like that, but C++ makes no such guarntee.
However, I (for some reason) have to pass in the count of the number of
arguments, or manually pass in NULL as the last argument. Otherwise, the
loop that extracts arguments from the 'va_arg' list runs past the end.

*** Should I *have* to manually pass in NULL or the number of arguments
in order to pass in a variable number of parameters using 'va_arg'?


You have to do something to indicate the number of arguments. Passing in
NULL or the number of arguments are two obvious ways.

john
Jul 19 '05 #2
this is what <stdarg.h> has to say:

typedef char * va_list;

#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) -
1) )

#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define va_end(ap) ( ap = (va_list)0 )

so, i believe you should pass some kind of a marker or number of arguments
(in your case a 0 as the last argument)
"Suzanne Vogel" <su*************@hotmail.com> wrote in message
news:3f**********@news.unc.edu...
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers
of parameters to functions. The example of its use given in the
Dinkumware documentation *seems* to imply that the 'va_arg' list should
*automatically* be terminated with NULL past the user's last argument:
http://www.dinkumware.com/manuals/re...&h=stdarg.html

However, I (for some reason) have to pass in the count of the number of
arguments, or manually pass in NULL as the last argument. Otherwise, the
loop that extracts arguments from the 'va_arg' list runs past the end.

*** Should I *have* to manually pass in NULL or the number of arguments
in order to pass in a variable number of parameters using 'va_arg'?

Below is my test example, with the function call in main():

/************************************************** *************************
*** * Demonstrate the use of variable numbers of parameters in a function call. ************************************************** **************************
**/ #include <stdarg.h>
#include <iostream>

/************************************************** *************************
*** * Declare a struct for use in avg() function.
************************************************** **************************
**/ struct Double {
double value;
Double(double v) {
value = v;
}
};

/************************************************** *************************
*** * Return the average of all parameters following 'n'.
*
* double avg(Double* x, [Double* x1], [Double* x2], ...)
************************************************** **************************
**/ double avg(Double* x, ...) {
// Pass parameters as Double* instead of double or Double& because the // following types are disallowed:
// * any array type
// * any function type
// * type float (e.g., double)
// * any integer type that changes when promoted
// * a reference type [C++ only]

va_list ap; // declare the parameter list
va_start(ap, x); // start reading the parameter list

double sum = x->value; // initialize sum and count
int count = 1;
while (Double* d = va_arg(ap, Double*)) {
sum += d->value;
count++;
}
va_end(ap); // stop reading the parameter list

return sum/(double)count; // return the average of all the values
}

/************************************************** *************************
*** * Main function.
************************************************** **************************
**/ int main(int argc, char** argv) {
// NOTE: I should *not* have to pass in NULL at the end, but otherwise // the loop inside avg() continues 3 places past the end until it
finally
// reads in a NULL.
std::cout << avg(&Double(1.0), &Double(2.0), &Double(3.0),
&Double(4.0), &Double(5.0), &Double(6.0), NULL) <<
"\n";
}

Thanks,
Suzanne

Jul 19 '05 #3

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

Similar topics

3
by: srinivas reddy | last post by:
Hi, I have following questions. 1. Does va_arg allow one to read user defined types. My compiler allows but I am wondering whether it is true for all. 2. I wrote the following code. Pardon my...
7
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double...
10
by: Richard Heathfield | last post by:
I was recently asked whether it is legal to start processing a variable argument list a second time, /before/ you've finished with it the first time. (I have no idea why the questioner might want...
5
by: matevz bradac | last post by:
Hi, I'm trying to implement delayed function execution, similar to OpenGL's display lists. It looks something like this: beginList (); fcn1 (); fcn2 ();
1
by: Chul Min Kim | last post by:
Hi, I'm writing logging functions. I want to use them within my demon program. I'm working on Solaris 7, Sun Sparc Machine. Here are source code....
4
by: Kufa | last post by:
Hi, I might post a stupid question, but i prefer give it a shot, as i tried several things without any success. In a function called with a varying number of arguments of varying types, could...
5
by: shaanxxx | last post by:
i have statements printf("%f",(double)1); /* works fine although i have sent %f for double */ printf("%c",(int)64); /* works fine although i have sent %c for int */ Need your comment on...
3
by: John Shell | last post by:
Hello, all. The following code results in a C2666 error (2 overloads have similar conversions). class FSVec2D { public: FSVec2D() { // code omitted }
5
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote: The relevant paragraph from the Standard is: ---- Begin Quote ---- The type declared is va_list which is an object type suitable for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.