0
Pass-by-reference problem in Codeblocks
Hi! I tried running this code in Codeblocks: #include <iostream> using namespace std; int fct(int *a){ *a=5; return *a; } int main(){ int a=3; cout << a << endl << fct(&a); return 0; } The output was: 5 5 Does anyone know why the value of "a" gets changed before calling the function? If I replace "cout << a << endl << fct(&a);" with "cout << a << endl; cout << fct(&a);" the output becomes: 3 5
2 Respostas
+ 1
Hi,
When u use cout << a << endl << fct(&a);
The computer will calculate the full string before to print it.
so it could look like that cout << [a << endl << fct(&)]
So the a is change in the function fct who is call before cout.
0
Thank you!