About function parameter passing-pointer passing and reference passing
Today, I have discussed with my colleagues the issue of pointer and reference transfer. Some ideas are correct in terms of reasoning. However, because it is reasoning, it is not so confident to express your opinion, based on the principle that practice is the sole criterion of truth, I typed several pieces of code on the computer to verify the correctness of reasoning. First go to the code and then analyze it.
Code 1:
Void Swap0 (int a1, int b1) {int temp; temp = a1; a1 = b1; b1 = temp;} void Swap1 (int * a1, int * b1) {// exchange address int * temp; temp = a1; a1 = b1; b1 = a1;} void Swap2 (int * a1, int * b1) {int temp; temp = * a1; * a1 = * b1; * b1 = temp;} void Swap3 (int & a1, int & b1) {int temp; temp = a1; a1 = b1; b1 = temp;} int main () {int a = 1, B = 2; Swap0 (a, B); cout <"Swap0 :"
<运行结果:
Examples/samples + samples/so6zL + samples/samples + 1xMr9vt29 + samples/ejrLDZtsiw2b/GveLKzbXju/e08r + qwbS906Opy/ yy2df3tcTKx9Xiv + nE2rTmo6zL + dLUy/weight + weight/weight + 1 xLHww/weight/0rvR + aOsy/weight + CjxwcmUgY2xhc3M9 "brush: java; "> typedef struct {int a;} A; void f1 (A * p) {p-> a = p-> a + 1 ;} void f2 (A * p) {(* p ). a = (* p ). a + 1 ;}int main () {A p; p. a = 1; f1 (& p); cout <"f1 p. a = "<