Complete the C pointer-Article 5: Passing function parameters (value transfer, address transfer, and reference transfer)

Source: Internet
Author: User


Complete the C pointer ---

Article 5: Passing function parameters

Article Structure

:

|

| ---- One or three questions

|

| -- Question 1

| -- Question 2

| -- Question 3

|

| ---- 2. function parameter passing method: Value passing

|

| --1. An error message about value transfer

| --2. A preparation knowledge

| --3. Understand the form of value transfer

|

| ---- 3. function parameter transfer method 2: Address Transfer

|

| ---- 4. function parameter transfer method 3: Reference Transfer

|

| ---- 5. Comparison between the last pass-through and reference Transfer


I. Three questions
Before opening the lecture, I would like to ask you three questions.

1. Question 1:The program code is as follows:

void Exchg1(int x, int y)  {  int tmp;  tmp=x;  x=y;  y=tmp;  printf(“x=%d,y=%d\n”,x,y)}void main(){  int a=4,b=6;  Exchg1 (a,b) ;  printf(“a=%d,b=%d\n”,a,b)}

Output result:
X = ____, y = ____
A = ____, B = ____
Ask what the underline part should be. Please complete.

2. Question 2:The Code is as follows.

Exchg2(int *px, int *py){  int tmp=*px;  *px=*py;   *py=tmp;  print(“*px=%d,*py=%d\n”,*px,*py);}main(){  int a=4;  int b=6;      Exchg2(&a,&b);      Print(“a=%d,b=%d\n”, a, b);}

The output result is:
* PX = ____, * py = ____
A = ____, B = ____
Ask what the underline part should be. Please complete.

3. Question 3:

Exchg2(int &x, int &y){   int tmp=x;   x=y;   y=tmp;  print(“x=%d,y=%d\n”,x,y);}main(){  int a=4;  int b=6;      Exchg2(a,b);      Print(“a=%d,b=%d\n”, a, b);}

Output result:
X = ____, y = ____
A = ____, B = ____
Ask what the underlined part of the output should be. Please complete.

You are not on the machine. Can you try it? How sure are you about the answers you wrote? The answer is correct. Do you want to know? (Well, let me tell you slowly !)
Well, let's talk about it. Let's continue our exploration journey.
We all know that function parameters in C language are passed in three forms: value transfer, address transfer, and reference transfer. Question 1: value transfer, question 2: Address Transfer, and question 3: Reference transfer. However, I was confused by the passing of these parameters. I believe many people share the same feelings with me?
Next, let me talk about these three transmission modes one by one.

Ii. function parameter passing method: Value passing

1. An error message about value transfer
Let's take a look at the definition of the exchg1 function in question 1:

Void exchg1 (int x, int y) // In the definition, the X and Y variables are called the form parameters of the exchg1 function {int TMP; TMP = x; X = y; y = TMP; printf ("x = % d, y = % d \ n", x, y )}

Q:What do you think this function is doing?
A:Does it seem to be a logarithm of the values of X and Y?
For more information, see the following procedure:

Void main () {int A = 4, B = 6; exchg1 (a, B) // A, and variable B is the actual parameter of the exchg1 function. Printf ("A = % d, B = % d \ n", a, B )}

Q: What will the printf ("x = % d, y = % d/N", x, y) Statement in exchg1 () output?
I will ask again: what is the output of the printf ("A = % d, B = % d/N", a, B) statement after exchg1?
The output result of the program is:
X = 6, y = 4 A = 4, B = 6 // Why not a = 6, B = 4?

Strange, clearly I substituted a and B into X and Y respectively, and completed the exchange of two variable values in the function. Why is, the value of the B variable is still not exchanged (still a = 4, B = 6, rather than a = 6, B = 4 )? If you have this question, it is because you do not know the relationship between real parameters A and B and the form parameters X and Y.

2. A preparation knowledge
To illustrate this problem, I will first provide a code:

int a=4;int x;x=a;x=x+3;

No. Now I ask you: what is the final a value and what is the X value?
(How to solve this problem? Give me this pediatric question. It's not easy, not just a = 4 x = 7 !)
In this Code, you need to understand that although the value is assigned to X, the variable is not an X variable. Any modification to X will not change the variable. Haha! Although simple and take it for granted, it is a very important thing to know.

3. Understand the form of value transfer
Check the code for calling the exclusive function:

Main () {int A = 4, B = 6; exchg1 (a, B) // The exchg1 function printf ("A = % d, B = % d ", the operation code completed when a, B)} exchg1 (A, B) is as follows. Int x = A; // struct int y = B; // the first two rows indicate the implicit operation int TMP; TMP = x; X = y when the function is called; y = TMP;

Note that the first two sentences are added to the call to execute the exchg1 function:

int x=a;int y=b;

This is two implicit actions when a function is called. It does exist. Now I just explicitly write it out. The problem becomes clearer. (Now you think the switching operation in the function is A, B, or just X, Y ?)
Originally, in fact, when the function is called, the values of the real parameters A and B are implicitly assigned to X and Y respectively. Then, in the exchg1 function you wrote, there is no a for it again, B has performed any operations. Only the X and Y variables are exchanged. It Is Not A or B. Of course, the value of A and B has not changed! The function only transfers the values of A and B to X and Y by assigning values. All the operations in the function are X, and the values of Y are not the values of A and B. This is the so-called parameter value transfer.
Haha, finally, I understand that it is precisely because it implies the two assignment operations that lead us to the aforementioned confusion (think that A and B have replaced X, Y, for X, Y is an operation on a and B. This is a wrong idea !).

 

Iii. function parameter transfer method 2: Address Transfer
Continue -- Address Transfer Problem!
Code for Question 2:

Exchg2(int *px, int *py){  int tmp=*px;  *px=*py;  *py=tmp;  print(“*px=%d,*py=%d\n”,*px,*py);}main(){  int a=4;  int b=6;      Exchg2(&a,&b);      Print(“a=%d,b=%d\n”, a, b);}

The output result is:
* PX = 6, * py = 4 A = 6, B = 4

Look at the Interface part of the function: exchg2 (int * PX, int * Py). Note that the PX and Py parameters are pointers.
Let's look at the call: exchg2 (& A, & B );
It substitutes the address of a (& A) into PX, and the address of B (& B) into py. Like the above value transfer, two implicit operations are performed during the function call: The & A, & B values are assigned to PX, py.
Px = &;
Py = & B;
Haha! We found that it is actually no different from passing a value, except that the address values of A and B are passed to PX and Py, instead of the contents of A and B, and (please make a comparison)
The entire exchg2 function call is executed as follows:

Px = & A; // py = & B; // note that these two rows call the implicit action of exchg2. Int TMP = * PX; * PX = * py; * py = TMP; print ("* PX = % d, * py = % d \ n", * PX, * Py );

In this way, there is an implicit value assignment operation for the first two rows. We can now see that the pointer PX and Py values are the address values of the and B variables respectively. Next, the operation on * PX and * Py is of course the operation on the and B variables themselves. Therefore, the exchange in the function is the exchange of values A and B. This is the so-called address transfer (the address for passing a and B is sent to PX and Py ), do you understand now?

Iv. function parameter transfer method 3: Reference Transfer
Code for Question 3:

Exchg3 (Int & X, Int & Y) // note that the format of the formal parameter at the definition is different from the value transfer {int TMP = x; X = y; y = TMP; print ("x = % d, y = % d \ n", x, y) ;}main () {int A = 4; int B = 6; exchg3 (, b); // Note: The call method here is the same as the value transfer print ("A = % d, B = % d \ n", a, B );}

Output result:

X = 6, y = 4A = 6, B = 4 // The output result is different from the value passed.

No. The code format is different from the value transfer, that is, in the definition:
Exchg3 (Int & X, Int & Y ).
However, we found that the values of A and B were reversed. This shows that the variables A and B are modified in exchg3 (a, B), not just X and Y.
Let's first look at exchg3 (Int & X, Int & Y) in the definition of the exchg3 function ). The X and Y parameters are int variables. We can call a function (for example, exchg1 (a, B);) Like a value (for example, exchg3 (, B );). However, there is a get address symbol in front of X and Y &. With this, when you call exchg3, the function will replace A and B with X and Y respectively. We call X and Y to reference the variables A and B respectively. In this way, the operation in the function is actually the real parameters A and B, that is, the function can be directly modified to the values of A and B.

 

5. Comparison between pass-through and reference transfer:

1. function definition formats are different:
The value is defined as exchg1 (int x, int Y );
The reference is passed here: exchg1 (Int & X, Int & Y );

2. The same format is used for calling:
Value Transfer: exchg1 (A, B );
Reference transfer: exchg3 (A, B );

3. functions are different:
The function that passes values does not operate on the and B variables themselves, but only assigns the values A and B to X. In the y function, only the X and Y variables are operated on, instead of the and B variables, the value of A and B is not modified by the exchg1 function.
In the reference transfer exchg3 (a, B) function, A and B are used to replace X and Y respectively. The function operates on the and B variables. Therefore, the value of A and B can be modified in the function.

 

 

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.