By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,990 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,990 IT Pros & Developers. It's quick & easy.

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

P: n/a

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;
}
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
<Giatto Cardiacci> wrote:

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.

Jul 23 '05 #2

P: n/a
Giatto Cardiacci schrieb:
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
Jul 23 '05 #3

P: n/a
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).
Jul 23 '05 #4

P: n/a
"Giatto Cardiacci" wrote:
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).


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #5

P: n/a
Karl Heinz Buchegger schrieb:
"Giatto Cardiacci" wrote:
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).

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
Jul 23 '05 #6

P: n/a
<Giatto Cardiacci> wrote:

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

Jul 23 '05 #7

P: n/a
Michael Wohlwend wrote:
{
sum += va_arg(args)


please ignore error like that :-)

should be:
sum += va_arg(args, double);
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.