0

How does "a" become 4, or it's mistake? I thought it should be 2 O.o

int x(int &a,int &b){ a = 3; b = 4; return a+b; } int main(){ int a = 2; int b = 7; int c = x(a,a); cout << a << b << c; } RESULT: 478

11th Apr 2018, 5:41 PM
James Anderson
James Anderson - avatar
4 Réponses
+ 1
I'd say you need to return a integer in the x method or make it an void.
11th Apr 2018, 5:53 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
Yes I get it, tnx :)
11th Apr 2018, 6:19 PM
James Anderson
James Anderson - avatar
0
yes, i forgot to write it, but it's not the problem
11th Apr 2018, 6:01 PM
James Anderson
James Anderson - avatar
0
ok using int &b or int &a in int x(...) you create an int with the same address as a in int main(), so a and b in int x(...) and the other a, too, are the same, but the last assignment is b = 4, which sets all three ints to the value 4 I hope you get what I mean
11th Apr 2018, 6:12 PM
Aaron Eberhardt
Aaron Eberhardt - avatar