Parameter transfer between functions

1 by value

 1 int test(int x)
 2 {
 3     x = x + 1;
 4     cout << x << endl;
 5     cout << &x << endl;
 6     return x; 7 } 8 9 int main() 10 { 11 int a = 3; 12  test(a); 13 cout << a<< endl; 14 cout << &a <<endl; 15 return 0; 16 }

operation result

4
000000EB483BFA70
3
000000EB483BFA94

Namely: calling the function assigned to the parameter storage unit in the stack, and assignment, and operation parameter argument independent

 

2 pass-by

 1 int test(int *x)
 2 {
 3     *x = *x + 1;
 4     cout << *x << endl;
 5     cout << &x << endl;
 6     return *x; 7 } 8 9 int main() 10 { 11 int a = 3; 12 test(&a); 13 cout << a<< endl; 14 cout << &a <<endl; 15 return 0; 16 }

operation result

4
0000002BF814F980
4
0000002BF814F9A4

Namely: calling the function assigned to the stack pointer points to the arguments, the cell pointer calculates, will change the value of the argument

3 pass by reference

 1 int test(int &x)
 2 {
 3     x = x + 1;
 4     cout << x << endl;
 5     cout << &x << endl;
 6     return x; 7 } 8 9 int main() 10 { 11 int a = 3; 12  test(a); 13 cout << a<< endl; 14 cout << &a <<endl; 15 return 0; 16 }

operation result

4
000000D1FF94F964
4
000000D1FF94F964

That is: the references in the calling function arguments a, x corresponds to the argument a reference to another, the memory is not allocated on the stack, the parameter x of the operating parameter corresponds to a real operation, and the operation of the saving stack

2020.4.4

 

Guess you like

Origin www.cnblogs.com/xiaofengyu96/p/12635471.html