C ++ Getting Started (2) - Initial Probe (Part)

zhaozj2021-02-11  199

It seems that from the ancient C era, the pointer began to become a difficult point in the minds of the masses; in today's C , object-oriented, template and other technologies have complex the difficulty in the past C, the difficulty of learning, but the pointer The content still occupies the position of "difficult to point". The reason is that a considerable part of C / C is transferred from the "advanced" language of "advanced". It is even more than too many programming experience from zero start. The pointer belongs to one of the most "low" parts of C / C , and it is also a heavy drama, the trick is more, and the beginners who don't know the underlying will be dizzy when I have been in this. Inevitable. In this section, we tried to spoken from some basic relative underlying principles, hey, I would like to help you see the official.

0. Let's talk about the variable 0.talk about variable

Ah, variable, it is a familiar concept. In most advanced languages, variables are identifiers consisting of letters and some allowed symbols (numbers, underscores, etc.), which can record useful data, and can participate in operations, re-assapore ... Well, it is true A friend. But if you want to learn C , you have to know more about this old friend. State from the variable, for example, we write:

INT main () {Int a; // ... return 0;}

Obviously, in the first line of the main program, we declare and define a integer variable A, the system (exactly the compiler) see this line, you will do two things: 1. Assign a small piece to accommodate integer The value of the value is specifically used for storage variables a; 2. Only remember the first address of this memory, and your procedure as long as you use A, the system will find that the memory, then press your Requires the corresponding operation ... 噫, you know, memory is divided into a lot of storage units, just like each household in the city, each unit (home) has a unique number called address value. (House number) is different from other units, to access a unit (hue, huh, huh), to perform index (visits) through the address value (house number).噫, so far these operations are in the dark, so we have to write a simple and clear procedure. However, if we are curious to see, C is also allowed. For example, we want to view the address value of A, you can use the value operator &, as long as you add &, you can get its address:

#include

Using namespace std;

INT main () {Int a; a = 3; cout << "a =" << a << '/ n'; // First information associated with variables: Variable value cout << "Address of A: "<< & a << endl; // Second information related to variables: variable // address value Return 0;}

Here is the result of my machine just output: a = 3Address of A: 0x241ff5c

There should be no problem with A = 3 output, but address of a, different machines, even the same machine different times, this is very understandable: After all, the memory layout is always changed, the system is assigned to you. A space, you can use it, don't be too picky. Also, is "0x241ff5c"? Is it somewhat whim? This is because the output of the address uses 16 credits, so it seems to be more mysterious. In fact, there is only an integer in the bones, you have to see if you use a decimal output, which involves the format of the output stream, this is not Say more (yourself check book ^ _ ^). Now, as long as you give any variables (or objects, it is registered), we can understand its specific storage address via & operators. Then, in turn, if we know the address of a variable, how do you turn to a variable operation? The answer is the same as the "*" operator, and "&" operators. It is also before adding an address value. As a result, we have obtained the variable corresponding to the address, which is often referred to as a decix (dereference), " * "It is the solution reference operator:

#include

Using namespace std;

INT main () {Int a; a = 3; cout << "A =" << a << '/ n'; //, the same ... Cout << "Address of A: << & a << '/ n'; //, the same as front ... Cout << "Get Back A:"; cout << * (& a) << '/ n'; // first use & A to get A address, then use * Will be returned by the address to the emission amount A * (& a) = 5; //, the variables obtained by * can be used as the original variable, and the regular operation COUT << "now, a =" << a << endl; // verification A is really change Return 0;}

This funny program output is as follows (Similarly, the address value is just a possible version):

A = 3ADDRESS OF A: 0x241ff5cget back a: 3Now, a = 5

Oh, the reason why it is funny because we first add a subsite resolution alphector, and then use the anti-focus operator from the address to be worth returning to A. There is no big development in the actual development of such a direct curve to save the country. However, this funny program is quite true to let us understand that the value operator & anti-reference operators * is a pair of reverse brothers, the former gets its address by the variable, and the latter gets its corresponding variable. If both are administered simultaneously, that is, * (& a) in the program, we have a circle, and I have to return to A itself.

1. Pointer 1.Pointer

Now we know the new value type in C : address value, it is integrated with integer, floating point, double precision, character, etc., is the important type of program composition operation, just most of which are hidden in most. Behind the program language. We also know that all values ​​such as integer, floating-point types correspond to integer variables / constants, floating-point variables / constants to store them, whether the address value can be stored with corresponding variable / constants? The answer is yes. Variables for storing address values ​​in C / C are referred to as pointer variables / constants, referred to as pointers. This is a quite figure: the address value stored in the pointer is usually "refers to a particular storage space, with a pointer boot, we can access the corresponding amount; and the address stored in the pointer variable is Through variable assignment, this makes pointers considerable flexibility; in addition, dynamic memory allocation is also inseparable from pointer ... These are all subsequent content, um, slowly, we still study how to declare pointers variable. For plastic variables, we declare the keyword int to modify, such as int A; declare an integer variable name A, since A is modified by int, so A has an intra-type variable attribute: It supports addition, subtraction, multiplication, etc. (not division), assignment, etc. So, if I add a few characters, becomes: int A, * p; then what does it mean? According to the above ideas, give you a five second time think: 1, 2, 3, 4, 5! OK, you can say it, we declare two integer Dongdong: A and * P, they all support addition, subtraction, multiplication, and complete operations (rather than division), assignment, etc. ... * P and A It should be equivalent, and they should all be able to represent a integer variable.噫, very good, this is what we infer entirely based on your own understanding. Now let's study: Since * p can represent a integer variable, then go to the *, what should P should mean? Remember the magical relationship of & and * what you said earlier? They truly opposite features: to add their address to the address, add * to obtain its corresponding variable. Now put the previous sentence, it becomes: to remove the address to the address to return to its corresponding variable (such as & a change back A), to remove the variable *, to return to its address (as mentioned earlier * (& a) is completely equivalent to the variable A. Remove * to becoming & A to get A 's address). Ok, we know that * p can represent an integer variable, so remove the * P, represent the address of * P, that is, P represents an address! Ha, now we can finally understand why a pointer is used by * rather than a & number or anything else. There is now a problem: Declaration (defined) variables There will be corresponding memory allocations, such as the system to see Int a; if you assign a block memory, so, for statement int a, * p; seemed to be A, * p The allocated memory of a stored integer.

Oh, more unfortunately, this time I haven't played the role of the logic, for C / C , int A, * P will make the system allocate a full value memory (no doubt), but for * p, due to * It is an operator that only monitors only, so the system is only the identifier P, which will be allocated to P. Not * P. Well, repeat it again: * Just help the system to understand the status of P, any declaration (defined), the system finally assigns the memory only the identifier itself, so the final P will get its memory. What kind of memory is received? Since * p can represent int type variables, we have just analyzed that P, represents the address value indicative of an int type variable, which connects them, that is, the memory is used to store a INT type variable. Address value --P is a variable that stores integer addresses - Hey, P is the pointer variable we envisaged earlier! Oh, I have a hard work, I finally got you! Well, the general C tutorial introduces the pointer to the direct introduction of its statement, and we are basically through logical reasoning, combined with the basis for the foundation of Section 0, to export the declaration method, such learning, I think it is more helpful to us Grasp the language substance at a more detailed level. This chapter shows more detailed, more words (I have limited effort, huh, huh), but thinking about it is not too complicated, only a few derivation chains, I hope that the scholars in the beginning of the first day will read it several times, read it Read the thin. Then ... relax, drink tea, listen to music, what is also tired of ^ _ ^), and then continue to tell the principle and some applications of the pointer. During the next, it is just a beginner and lovers like you, although ignorance, but I like to think about some miscellaneous problems. If there is any mistake, I still look out that I am grateful. If there is any suggestions and comments on this series, it is also very welcome. In short, I hope we can make progress together.

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

New Post(0)