Pass value, pass pointer, pass reference

Source: Internet
Author: User

The difference between value passing, pointer passing, and reference passing

The rules of the C language are simple:"All arguments are invocation of values." on the basis of this statement, we analyze the difference between value passing, pointer passing, and reference passing.

First, value passing

Value is passed, which is related to the nature of the C function. All parameters of the C function are passed in a "call-to- value" manner, which means that the function value is given a copy of the parameter value, and the function can safely modify the copy value without worrying about modifying the arguments actually passed to him by the calling program.
Let's take a look at implementing the function Swap1:

void swap1(int a,int b){    printf("\n\n传值的示例函数swap1\n");    printf("形参a的地址:%p\n",&a);    printf("形参b的地址:%p\n",&b);    int temp;    temp = a;    a = b;    b = temp;    printf("形参a的值:%d  ",a);     printf("形参b的值:%d  ",b); }

The main function is as follows:

    intb; A =5; b =6;printf("value of argument a:%d ", a);printf("Address of argument a:%p\ n", &a);printf("Value of argument B:%d ", b);printf("Address of argument B:%p\ n", &b); Swap1 (A, b);printf("\ n \ r value after value Exchange: \ n");printf("value of argument a:%d \ n", a);printf("Value of argument B:%d \ n", b);

The results of the implementation are as follows:

The address of argument A is: 0018ff44, and the address of argument B is 0018ff40

Stack-The program is automatically assigned by the compiler when it is run, storing parameter values for functions, values for local variables, and so on. It operates in a manner similar to a stack in a data structure. Automatically released by the compiler at the end of the program. Therefore, the parameter, a, b, is stored in the stack area. Under Windows, the stack is the data structure that extends to the low address, which is a contiguous area of memory. Therefore, we will see that the address of A/b is a decreasing trend.

Therefore, according to the idea of "value passing", the formal parameter is a copy of the argument, and the program will open a new stack as a formal parameter. They are exchanged in this new stack area, which does not affect the memory of the actual argument.

After executing the SWAP1 function, the value of a, b in the parameter's memory changes, but does not affect the value of the argument.

when a function is called, the argument is read from right to left, so the address of B is higher than the address of a

Second, pointer transfer

Let's look at implementing the function Swap2:

void Swap2 (int *a,int *b){printf("\ n \ nthe example function for pointer swap2\n");printf("Address of formal parameter a:%p\ n", &a);printf("Address of parameter B:%p\ n", &b);intTemp temp =*a;*a=*b;*b= temp;printf("value of parameter a:%p ", a);printf("value of parameter B:%p ", b);} Main function call: Swap2 (&A,&B);printf("\ n \ nthe value of A/b after a pointer exchange: \ n");printf("value of argument a:%d \ n", a);printf("Value of argument B:%d \ n", b);

Operation Result:

So we can see that the value of the formal parameter is actually the address of the argument, because C is the value of the pass, and the main function passes in the address of &a,&b, a, a, and so the address of A and B is stored in the formal parameter.

Then execute:

*a;*a*b;*b = temp;

What do you mean, *a? *a is the value of the function stored in memory with address a. So the above three statements mean that the function value stored in memory of address A is exchanged with the value of the function stored in memory with address B. Such as:

Therefore, he modifies the contents of the argument. The content of the arguments after execution should look like this:

So, at this point a value of 6,b is 5.

Third, reference delivery

Let's look at implementing the function Swap3:

void Swap3 (int&a,int&B) {printf("\ n \ nthe example function swap3\n");printf("Address of formal parameter a:%p\ n", &a);printf("Address of parameter B:%p\ n", &b);intTemp    temp = A;    A = b; b = temp;printf("value of parameter a:%d ", a);printf("Parameter B's value:%d ", b); }main function Call: Swap3 (A, b);printf("\ n" value of A/b after a reference exchange: \ n ");printf("value of argument a:%d \ n", a);printf("Value of argument B:%d \ n", b);

Execution Result:

We can see that the address of the formal parameter is the same as the address of the argument, which brings up the assembly code:

71:       swap3(a,b);0040D9B8   lea         eax,[ebp-8]0040D9BB   push        eax0040D9BC   lea         ecx,[ebp-4]0040D9BF   push        ecx0040D9C0   call        @ILT+15(swap3) (00401014)0040D9C5   add         esp,8

Lea Eax,[ebp-8] is the offset address of B is fed into EAX,

This is SWAP2 's assembly code, and we can see that the two are the same.

66 : Swap2 (&a,&b) Span class= "hljs-comment";  0040  D979 Lea Edx,[ebp-8 ]0040  d97c push  edx0040  d97d Lea Eax,[ebp-4 ]0040  D980 push  eax0040  D981 call  @ILT +10  (SWAP2) ( 0040100  f) 0040  D986  Add  esp,8   

So, for the swap3 here and the previous SWAP2, the function parameters in the stack are all addresses, and the way in which the functions are manipulated is consistent. However, for SWAP3 this address is the keynote function passed in by pressing the offset address of the argument variable-this is a reference pass, and for SWAP2, the address is the keynote function passed in by stacking the value of the argument variable-this is the value passing, Only because this argument variable is a pointer variable so its value is the address.
The key point here is that it is also an address, a variable address in a reference pass, and a value for a pointer variable in a value pass.

Iv. Summary

In fact, both the pointer passing and the value passing essence are value passing, and the value passing is a copy of the variable to be passed. There is a reference pass in C + +, that is, the address of the variable being passed to the called function, and if it is changed in the called function, it will be changed in the calling function.

Reference:
1. How the function parameters are passed
2. The seventh chapter of the C and the pointers function.

Pass value, pass pointer, pass reference

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.