'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