Board index » cppbuilder » How to call a function with variable argument list...

How to call a function with variable argument list...


2005-04-15 06:04:33 PM
cppbuilder28
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 AFunct2 ( int num, ... );
double average ( int num, ... )
{
va_list arguments; // A place to store the list of
arguments
double sum = 0;
va_start ( arguments, num ); // Initializing arguments to store
all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument
list to sum.
va_end ( arguments ); // Cleans up the list
return sum / num; // Returns some number (typecast
prevents truncation)
}
double AFunct2 ( int num, ... )
{
HOW to call "avarge" here? <----???????
}
double AFunct1 ( int num, ... )
{
HOW to call "AFunct2" here? <----???????
}
main ()
{
double d = AFunct1(5, 2, 3, 4, 123.456, 78);
cout << d;
}
 
 

Re:How to call a function with variable argument list...

<Lion Tu Ye>writes:
Quote
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 AFunct2 ( int num, ... );

double average ( int num, ... )
{
va_list arguments; // A place to store the list of
arguments
double sum = 0;

va_start ( arguments, num ); // Initializing arguments to store
all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument
list to sum.
va_end ( arguments ); // Cleans up the list

return sum / num; // Returns some number (typecast
prevents truncation)
}

double AFunct2 ( int num, ... )
{
HOW to call "avarge" here? <----???????
Not.
Functions with variable argument list are best avoided. In C++,
function overloading can be used to get to an equivalent solution, but
without the problems of variable argument lists.
 

Re:How to call a function with variable argument list...

<Lion Tu Ye>wrote in message news:425f91b3$ XXXX@XXXXX.COM ...
Quote
How to call a function with variable argument list from
another function again with variable argument list?
You have three options:
1) use inline assembly to build up the stack manually. I don't recommend
this one.
2) make copies of your functions that have va_list arguments instead of
varaible argument lists, and then call those instead of the originals:
double average(int num, ...);
double va_average(int num, ...);
double AFunct1(int num, ...);
double va_AFunct1(int num, ...);
double AFunct2(int num, ...);
double average(int num, ...)
{
va_list arguments;
va_start(arguments, num);
double a = va_average(num, arguments);
va_end(arguments);
return a;
}
double va_average(int num, va_arguments)
{
double sum = 0.0;
for(int x = 0; x < num; ++x)
sum += va_arg(arguments, double);
va_end(arguments);
return sum / num;
}
double AFunct2(int num, ...)
{
va_list arguments;
va_start(arguments, num);
double a = va_average(num, arguments);
va_end(arguments);
return a;
}
double va_AFunct2(int num, va_list arguments)
{
return va_average(num, arguments);
}
double AFunct1(int num, ...)
{
va_list arguments;
va_start(arguments, num);
double a = va_AFunct2(num, arguments);
va_end(arguments);
return a;
}
main()
{
double d = AFunct1(5, 2, 3, 4, 123.456, 78);
cout << d;
}
This is your best approach, unless you...
3) change the functions to not have variable argument lists to begin with.
Use arrays instead.
double average(int num, double[] arr);
double AFunct1(int num, double[] arr);
double AFunct2(int num, double arr[]);
double average(int num, double[] arr)
{
double sum = 0.0;
for(int x = 0; x < num; ++x)
sum += arr[x];
return sum / num;
}
double AFunct2(int num, double[] arr )
{
return average(num, arr);
}
double AFunct1(int num, double[] arr)
{
return AFunct2(num, arr);
}
main()
{
double arr[] = {2, 3, 4, 123.456, 78};
double d = AFunct1(5, arr);
cout << d;
}
Gambit
 

{smallsort}

Re:How to call a function with variable argument list...

On Fri, 15 Apr 2005 10:50:42 -0700, Remy Lebeau (TeamB) wrote:
Quote
3) change the functions to not have variable argument lists to begin with.
Use arrays instead.
In which case you might as well use std::vector
--
Good luck,
liz