1 registered members (AndrewAMD),
1,089
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Variable Arguments
[Re: Excessus]
#158493
10/07/07 10:22
10/07/07 10:22
|
Joined: Oct 2002
Posts: 4,753 Munich, Bavaria, South of Germ...
TripleX
Expert
|
Expert
Joined: Oct 2002
Posts: 4,753
Munich, Bavaria, South of Germ...
|
example for variable arguments: Code:
#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; }
|
|
|
Re: Variable Arguments
[Re: TWO]
#158495
10/07/07 14:23
10/07/07 14:23
|
Joined: Jan 2004
Posts: 2,013 The Netherlands
Excessus
Expert
|
Expert
Joined: Jan 2004
Posts: 2,013
The Netherlands
|
Seems like it simply doesn't work. The arguments aren't copied to the stack in case of "...". I wrote a little program that demonstrates it. Both functions tst1 and tst2 should return the integer that is stored after parameter a. This seems to work only for tst1. Code:
#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"); }
|
|
|
Re: Variable Arguments
[Re: Ottawa]
#158497
10/08/07 23:56
10/08/07 23:56
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
JCL stated, that this is not supported at the moment, because it required major changes in the way the compiler works. However, he stated he'd take care about this in the future.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|