Posted By: TWO
Variable Arguments - 10/03/07 16:02
JCL once postet a piece of code from a standard C book. With some fiddling I erased all compiler errors, but it produces a crash. So, any chance to get some variable args into my app?
#include <cstdarg>
#include <iostream>
using namespace std;
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)
}
int main()
{
cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;
cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;
}
#include <acknex.h>
int tst1(int a, int b)
{
return *((&a) + 1);
}
int tst2(int a, ...)
{
return *((&a) + 1);
}
int main()
{
if(tst1(1, 2) == 2)
printf("test 1 success");
else
printf("test 1 failure");
if(tst2(1, 2) == 2)
printf("test 2 success");
else
printf("test 2 failure");
}