Determine C ++ pointer (1)

zhaozj2021-02-16  147

Since 2001 University graduated, I started to be less than using C . The day for C crazy, I stayed forever in the familiar campus. In terms of programming languages, C is undoubtedly the most affinity and temptation. Although I have been involved in Java now, I have never changed it. Often germination will do something in C . Gradually, there is a thought of publishing an article online. "The C pointer in my eyes" is a bits and pieces from the corner that I have been forgotten from the corner that I have been forgotten from the corner that I have been invested by I am. This article is the first, and what is the essence of the pointer.

In a considerable part of the C tutorial, examples of this data exchange will be raised basically in the pointer. I am also willing to give this example as the beginning of the pointer.

Below is the class code content:

// ... is limited to the space, only some of the contents of the problem ...

Definition part:

PUBLIC:

Void Swap (Int A, INT B);

Implementation part:

Void CpointResearch :: swap (int x, int y) {

INT NTMP = X;

X = Y;

y = NTMP;

}

Here is the test data and results:

enter:

100, 200

Output (after executing the SWAP function):

100, 200

Press any key to continche

It can be seen very clear that there is no data exchange.

Let's wipe the programmer to write this way of this program: There are two small boxes, the frame is set to 100 cards, and the two boxes are 200 cards. The two cards are swapped in the position, become a frame The card stores the number 200, and the b is stored in a card with a number of 100.

Does this idea have a problem? The answer is clear, there is no problem with a little bit! If the idea is completely correct, he (she) is really doing so, the card exchange must be successful. But why is the above program simulates such an execution process, but it is not wanting? If you want to thoroughly understand this problem, we must understand the fundamental, that is, what is the essence of this problem, this is really solving the problem.

Let's take a deep way to analyze the seemingly simple imaginary scenes. First, there are two boxes, this is the fact that it is unquestionable. Second, the box is equipped with cards, and the above numbers are different, this is an indisputable fact. Third, during the exchange of cards, although two boxes have not been moved, the cards inside have moved. Fourth, the card moves from one box to another. It is the four points to ensure that the card is successful. In other words, if the execution switching process of the above data exchange program If the four conditions are met, the result of the computer execution will be the correct result we expect. Then, the reason for this error is clear, that is, in the process of computer executives, at least one point is at least one point. The following analysis.

First, from the input condition.

The input condition is int a = 100; int b = 200; we can see from these two C statements, two "boxes" have been, which are A and B, which means the first condition. In addition, the frame (a) placed the card (Int a = 100;), the Box (b) placed the number of 200 (INT B = 200;), this shows that the second condition also has . Other useful information that can not be seen from the input condition. At this time, we can already determine that there is a place where the problem is not satisfied. Second, look at the SWAP function.

Function INT NTMP = X; X = Y; X = NTMP; is the most typical code that implements data exchange only with three lines of code. No problem, you can see a clear kich before the execution of these three lines of code, and the output can be seen, and it can be exchanged. This indicates that the fourth condition above is also satisfied, because these three lines of code are "putting the X card from the A) to the ground, then put the Y card of the Box in the b box, it is best to put the card on the ground (ie X card) Pick up in the Box. " So, you can now explain that the problem is not satisfied in the third condition. We will continue to analyze.

Third, declare from the SWAP function.

From the program code exchanged program code, there is only one thing left. Our implementation process is actually a few steps:

First, enter: int a = 100; int b = 200; this is just assigning operations, certainly no problem.

Next, execution: Step 1, prepare the SWAP (INT X, INT Y) function, pass A to X, B transmission to Y.

Step 2, perform SWAP (X, Y), is to perform the contents of the function. Step 2 It has been verified above, no problem.

Finally, the execution of the A and B: 100, 200 in the main function, which is the output of the C standard output function, and what data is provided is set in the field. This is no problem.

At this point, the problem can be positioned, it is "step 1" in "Second"! The execution process of this step continues to subdivision, is: Place the number of the number of the Afro (input a) to the cobalt (parameter X) of the SWAP function Note 1, the number of the Box (B) is 200 cards are placed in the trunk (the parameter y) of the SWAP function. The program is connected to "step 2" in the "Second", which is also entered into the function, which is swapped in the card with the Cards of the Box and Dock. It can be seen that the original exchange is the Box and Ding box! What is it still what is the original frame and the B frame? This certain doesn't see the desired output.

So the root of solving the problem is to "find the box", only exchange the "box" you want to exchange. The true understanding of this "Finding Box" will directly reflect the true understanding of the pointer. This way, possible solutions such as:

Based on the SWAP method of the pointer (directly take the card and the carton of the B) and exchange, there is no cobalt and Ding box at all.

Void CpointResearch :: swap (int * x, int * y) {

INT NTMP = * x;

* x = * y;

* y = NTMP;

}

Based on the reference SWAP method (not directly used to Box (but reference to AR) (but referenced): void cpointResearch :: swap (int & x, int & y) {

INT NTMP = X;

X = Y;

y = NTMP;

}

Note 1: The imperative example of this in this paper is mainly for explanation, but the example is not necessarily very appropriate. "Put the number of the numbers of the A-frame (input a) to a Class A (parameter x) of the SWAP function, more appropriate, it should be" the box has the card with lines in the number 100 card On the upper, "A pass to X" operation is now executed, that is, the Co-frame is to pull a line to the card on the number 100. "

This article emphasizes that "What is the nature of the pointer?" The nature of understanding the pointer is the key to learning pointers. Especially the first school pointers, if you can truly understand this SWAP function, the understanding of the pointer should say that there is already a certain substrate. If it is very impetuous or beautiful, look at the pointer, it will not really master the rich connotation of the pointer. With pointers, it is necessary to use, "If you don't have a pointer, you don't know how to be good". Otherwise, it is not too much to use the pointer, and it is easy to make mistakes. I don't know how to eliminate it, it may not be high.

转载请注明原文地址:https://www.9cbs.com/read-8472.html

New Post(0)