I m trying to learn C++; the problem comes with variable arguments the program is written as below:-
using namespace std;
int sum(int count,...)
cout << sum(2,4,6) << endl;cin.get();
The output from
sum(2,4,6) should be 10 not 12 ... your first parameter is a counter for the rest of the parameters. If you want the answer to be 12 you should call
For sum(2,4,6), 10 is correct result.
For sum(2,4), 6 is wrong.
The first argument (count) of the sum function tells how many numbers will follow, so ...
sum(2,4,6) tells to sum 2 numbers which values are 4 and 6, for a total of 10.
sum(2,4) tells to sum2 numbers, but the second is not provided, so it will be read from a wrong memory location (probably from the stack location where the first argument, currently 2, is stored).
sum(2,4) should be replaced with sum(1,4).
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)