C Language: Value passing, address passing and reference passing (example: Value Exchange)

Source: Internet
Author: User

I personally understand the value passing, address passing, and reference passing in C language.

Through an example: Swap (Swap the value of two integer variables) to express!

1#include <stdio.h>2 voidSWAP1 (intAint*b);3 voidSWAP2 (int& A,int&b);4 voidSWAP3 (intAint*b);5 6 voidMain () {7printf"Hello world!\n");8     intA =3;9     intb =4;Tenprintf"bef Swap, add of a =%d\n",&a); Oneprintf"aft Swap, val of a =%d\n", a); A     //Swap (A, b); -Swap1 (&a,&b); -     //Swap2 (A, b); the     //swap3 (&a,&b); -      -printf"aft Swap, add of a =%d\n",&a); -printf"aft Swap, val of a =%d\n", a); + } - //Pass by Value + voidSwapintAintb) { A     inttemp =A; atA =b; -b =temp; - } - //Pass by Address - voidSWAP1 (intAint*b) { -     inttemp = *A; in*a = *b; -*b =temp; to } + //Pass by reference - voidSWAP2 (int& A,int&b) { the     inttemp =A; *A =b; $b =temp;Panax Notoginseng } - //pass by value? the voidSWAP3 (intAint*b) { +     int* Temp =A; AA =b; theb =temp; +}

The above function, four swap functions, outputs the result:

Swap (A, B):

Swap1 (A, B):

Swap2 (A, B):

SWAP3 (A, B):

We see that what really works is Swap1 and SWAP2. The two are address passing and reference passing. Swap is a typical value pass, and swap3 is what I'll talk about later.

Analysis!

0, value passing

This is relatively simple, argument a originally pointed to address 1638212, which represents the value of 1638212 this address is 3. In the swap function, the argument a copies the value to the parameter A, parameter A at this time also has the address in memory, the address = xxxx, the value is 3, in all function body operation, is the XXXX this address operation, so does not affect the actual parameter value.

1, Address delivery

This is difficult for students who can't clarify what the pointers are. Here we are accustomed to write pointers as int* a,int* b instead of int *a,int *b. We can understand this: the pointer is a special type of data, if int c = 5;int* A = &c; A is a pointer variable, its value is the address of C! The asterisk "*" is a value operation, and the number "&" is a fetch operation. So at this point, simply look at A and B are an integer, they represent the address, after the operation of the value can get the value of the corresponding address. The function accepts two variables of type pointer, and actually accepts A and B, that is, two addresses. So now analyze the function body:

1 int temp = *a; // Remove the value of address A and assign to the integer variable temp 2 *a = *b;      // take out the value of address B and assign this value to the value pointed to by address a 3 *b = temp;    // assigns the value of temp to the value pointed to by address B

Therefore, we see that the value of the corresponding address has changed because the function passed in the address, and the function body also takes the value of the address and assigns the value operation. But the address does not actually change, from the output of the function, the address of a does not change. In the C language, the function allocates memory addresses to each variable at run time, and the address cannot be changed once the variable is not destroyed. &a = &b; is not compiled through.

2, reference delivery

This is easier to understand, we understand the reference, the reference is an alias of the variable, and calling the alias is exactly the same as calling the variable. So the results of SWAP2 can be explained. It is important to note that because the reference is an alias, the reference is not a data type, and memory does not allocate memory for it alone, but instead invokes the variable it refers to directly. This is not the same as the address passing the pointer (that is, if a pointer is pointing to a variable, but the pointer variable has an address assignment in memory), the following code validates.

1 voidMain () {2printf"Hello world!\n");3     intA =3;4     intb =4;5     int* C = &a;//c is a pointer to a6     int& d = b;//D is a reference to B, alias of B = d7printf"val of a =%d\n", a);8printf"add of a =%d\n",&a);9printf"val of C =%d\n", c);Tenprintf"add of C =%d\n",&c); Oneprintf"val of b =%d\n", b); Aprintf"add of B =%d\n",&b); -printf"val of d =%d\n", d); -printf"add of D =%d\n",&d); the}

Output Result:

We see that the value of C is the address of a, the address of C is assigned separately, and the value of D is the value of B, and the address of D is the address of B!

4, about swap3 how to explain.

I think SWAP3 is a value pass, and if we take int* completely as a data type with int, then the SWAP3 and swap two functions are the same. But the latter passed in the copy value of the variable A, a, and the latter passed in the copy value of the address of the variable A, B, which cannot be reflected externally, nor can it.

Finally, we note that for applications, if we have code: int a = 3; int& B = A; (b is an alias of a) b = 10; Then we will find that the "a" also becomes 10.

But in Java, if we simply think of Java references as a reference here, there is a problem. Because if a function is in and out of an object person person = new Person ("ZHANG San"), do this in the body of the function: person = new Person ("LI Si"); then the value of person cannot be changed. So we say that the function passing of Java is value passing.

C Language: Value passing, address passing and reference passing (example: Value Exchange)

Related Article

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.