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

G

Guest

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;
}
 
R

Rolf Magnus

How to call a function with variable argument list from another function
again with variable argument list?

There is no way to do that.
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;
}

Make a version of your average function that takes a va_list.

double vaverage(int num, va_list arguments)
{
double sum = 0;
for (int x = 0; x < num; x++)
sum += va_arg(arguments, double);
return sum / num;
}

double average ( int num, ... )
{
va_list arguments;
va_start(arguments, num);
double sum = vaverage(num, arguments);
va_end(arguments);
return sum;
}

double AFunct2(int num, ...)
{
//use vaverage instead of average
}

Generally, it's best to avoid variable argument lists in C++, since they are
not type safe and very restrictive and error-prone.
 
?

=?ISO-8859-15?Q?Stefan_N=E4we?=

Giatto said:
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;
}

Why not use a std::vector<double> to store the numbers to be averadged.

Stefan
 
G

Guest

Why not use a std::vector<double> to store the numbers to be averadged.

Stefan


The function "avarage" was just for an example.
What I want to do is to be able to send variable number of parameters of
variable type to a function (simiar to good-old printf function).
 
K

Karl Heinz Buchegger

Giatto Cardiacci said:
The function "avarage" was just for an example.
What I want to do is to be able to send variable number of parameters of
variable type to a function (simiar to good-old printf function).

Even in this case I would try to come up with a container that holds
all those arguments.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Karl said:
Even in this case I would try to come up with a container that holds
all those arguments.

Right.
Or, if there's only a limited range of number of arguments (i.e. 2..5
arguments) use function overloading.

Stefan
 
M

Michael Wohlwend

How to call a function with variable argument list from another function
again with variable argument list?

something like that worked for me:

double average ( int num, va_list args )
{
for (...)
{
sum += va_arg(args)
}
return sum;
}

double AFunct1 ( int num, ... )
{
va_list args;
va_start(args, num);
ret = average(num, args);
va_end(arrgs);
return ret;
}



Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top