About function parameter passing-pointer passing and reference passing

Source: Internet
Author: User

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: "<A <" B: "<B <Endl; swap1 (& A, & B); cout <" swap1: "<A <" B: "<B <Endl; swap2 (& A, & B); cout <" swap2: "<A <" B: "<B <Endl; A = 1; B = 2; swap3 (a, B); cout <" swap3: "<A <" B: "<B <Endl; return 1 ;}
Running result:

For swap0 (INT A1, int B1), when this function is called, values a = 1 and B = 2 in the main function will pass 1 and 2 to swap0 () a1 and B1 in, A1 and B1 in swap0 () are the local variables of this function. They only accept the values 1 and 2, in the function, the values of these two local variables are exchanged and there is no association with A and B in main (), that is, the values of A and B in main () are not affected, after swap0 is executed, the output values A and B are 1 and 2.

Swap1 (int * A1, int * B1) and swap2 (int * A1, int * B1) have the same parameters, what's different is that swap1 functions operate on pointers internally, while swap2 uses unreference *. What's the difference? From the memory perspective: the main function defines a and B and assigns values to 1 and 2. It can be understood that the system allocates a block of memory for a to store 1 and B to store 2, assume that the starting address of the memory allocated to a is 1000, and that of B is 1004, that is, & A = 1000, & B = 1004. Call swap1 (int * A1, int * B1) and pass the two addresses to A1, B1, a1 = 1000, b1 = 1004, respectively, the value of the pointer variable is switched to a1 = 1004, b1 = 1000, and A1 and B1, there is no impact on the memory of a and B, so the output result is a = 1, B = 2. However, swap2 is different. It understands the reference operation. * A1 indicates that he has obtained the value of the 1000 address, and perform operations on the data on this address (the key is to understand this unreference operation, Baidu encyclopedia explains and click to open the link). It operates on this memory, so it can successfully modify the values of A and B.

About swap3 (Int & A1, Int & B1), it is a transfer reference, reference is the alias of the variable, and the two variables A and B are the same, so a can be changed, the value of B.

Code 2:

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="<<p.a<<endl;    p.a=1;    f2(&p);    cout<<"f2 p.a="<<p.a;    return 1;}

Running result:

Regarding F1 (A * P), it also transmits the address. In the function, the symbol "->" is used to operate on the data in the memory corresponding to the address, therefore, you can modify the value of a member. F2 (A * P) is also a non-reference operation, see swap2 above.

If any error occurs, thank you for your correction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.