I have already prepared the book "C + + Primer", if this "C + + Primer Plus" continues to be so slapstick, I will change the lead ~!
Yes, this book even if not yet introduced, began to move the pointer out, although it is simply introduced.
This catalogue arrangement I also woke up.
So, according to the progress of the book, today comes a preliminary talk about pointers.
But today, Friday, you know, just water, because I'm afraid to forget it by Monday.
1. Enumeration
So, what is an enumeration? (Jor: Wait ~! What about the good pointers? )
Most high-level languages should have enumerations, so there's nothing to introduce.
The use is this:
Copy Code code as follows:
Defined
Enum Monstertype {
Enmonster_nor,
Enmonster_hard,
}
Declaring an enumeration variable
Monstertype Mtype;
Use
if (mtype = = Enmonster_nor) {
Log ("Go to Hell" ~! ");
}
else if (type = = Enmonster_hard) {
Log ("Oh, hello, this is my salary this month, all to you, oh, I adore you, you are the most of my life." (10,000 words omitted) ");
}
2. What is a pointer
Because this is not a real C + + tutorial, just a supplement, so the basic concept I do not say.
A pointer is something that can point to the memory space, yes, it just points to the memory space, it is not the memory space itself.
Okay, no nagging.
3. Declaring pointers
Declaring a pointer variable is a simple thing, like the following code:
Copy Code code as follows:
Yes, even our previous normal variable also has a memory address, using the & symbol to get its content address.
Our pointer variable p is then used specifically to store the memory address.
When declaring a variable, add the * number to the code this is the pointer variable.
Remember, this is a statement ~ Then if you have not learned the pointer, it will be confusing.
4. Using pointers
How do you use the pointer? Quite simply, the following code:
Copy Code code as follows:
int num = 10;
int *p = #
*p = 20;
Std::cout << *p << "\ n";
Std::cout << num << "\ n";
Finally, *p and Num are all 20.
P is a pointer variable that holds the memory address of the NUM variable.
And *p is the value of this memory address, which is the value of our variable num.
So, *p = 20 means to change the value on this memory address to 20.
Here's the point:
1. Assuming num's memory address is 0XFFFFFF, the content stored on this address is the number 10
2.P is a pointer that holds the memory address of NUM, so the value of P is 0xffffff, and the content stored on this address is the number 10
The 3.*p represents the content stored above the memory address that P points to, that is, the number 10, in other words, the *p value is 10
4. Point to *p = 20, which is equivalent to changing the contents of the memory address pointed to by P to 20
5. Because Num's memory address is the address that P points to, the value that num saves in memory has changed
6. The value of the final num will also become 20
You get it? (Jor: Say it again in Chinese)
Well, once again in Chinese, that is:
Because the memory address that P points to is the memory address where NUM resides, the content on this address can be changed through num and *p.
In a word, at the moment *p is equivalent to num, you can do various operations (assignment, addition and subtraction, etc.)
(Jor: How good to speak Chinese earlier!) Do you have to flaunt your half-tone Japanese?! )
5. The pointer is rumored
Sometimes, we can declare pointers like this:
Copy Code code as follows:
int *num;
But you mustn't use that.
*num = 100;
At first glance, it seems normal to use it, just as it was said.
Note, however, that pointer num is not assigned a value when it is declared, that is, the pointer does not now know where it is pointing.
So, casually using such a pointer which does not know which point is likely to destroy our program.
For example, we wanted to declare a pointer to one of our hairs, but we didn't assign it a value.
So the pointer does not know where it is pointing, it may also point to where it has previously been pointed.
What if the pointer had been pointing to a place below our waist?
In case we use this pointer to "cut off the point of the pointer, cut it all."
Yes, I thought the hair was cut, the result! Cut the waist below!
What do you do for the rest of your life, you can't run anymore (where are you going?) )
6. The end of
Well, the water is here today ~ See you next time