Complete C language pointer Article 5

Source: Internet
Author: User

I. I would like to ask you to make three questions before you start the lecture. (Hey, you have to get your head down first ...... Who threw my eggs ?)

1. Question 1:ProgramCodeAs follows:

` VoidExchg1 (IntX,IntY){IntTMP;TMP = X;X = y;Y = TMP;Printf ("x = % d, y = % d \ n", x, y)}VoidMain (){IntA =4, B =6;Exchg1 (A, B );Printf ("A = % d, B = % d \ n", a, B)} `

Output result:

X =__ 6 __, y =__ 4 __

A =__ 4 __, B =__ 6 __

2. Question 2: the code is as follows.

`Exchg2 (Int* PX,Int* Py){IntTMP = * PX;* PX = * py;* Py = TMP;Print ("* PX = % d, * py = % d \ n", * PX, * Py );}Main (){IntA =4;IntB =6;Exchg2 (& A, & B );Print ("A = % d, B = % d \ n", a, B );} `

The output result is:

* PX =__ 6 __, * py =__ 4 __

A =__ 6 __, B =__ 4 __

3. Question 3:

`Exchg2 (Int& X,Int& Y){IntTMP = X;X = y;Y = TMP;Print ("x = % d, y = % d \ n", x, y );}Main (){IntA =4;IntB =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:In C language, function parameters are transmitted 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:

` VoidExchg1 (IntX,IntY)//The X and Y variables in the definition are called formal parameters of the exchg1 function.{IntTMP;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?

`VoidMain (){IntA =4, B =6;Exchg1 (A, B)//The A and B variables are the actual parameters 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?
Q: 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:

` IntA =4;IntX;X =;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 (){IntA =4, B =6;Exchg1 (A, B)//The exchg1 function is called here.Printf ("A = % d, B = % d", a, B)} `

The operation code completed when exchg1 (A, B) is as follows.
Int x = A; // second
Int y = B; // ignore note that the first two rows are implicit operations when the function is called.
Int TMP;
TMP = X;
X = y;
Y = TMP;

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

` IntX =;IntY = 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, what do you think is the, B, or only X and Y variables in the function ?)
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. Not a, 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 !).

Related Keywords:
Related Article

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.