C++ basic introductory tutorial (iv) : enumerations and Pointers

  • 2020-04-02 02:53:38
  • OfStack

I have prepared the book "C++ Primer", if this "C++ Primer Plus" continue to be so silly, I will change the protagonist ~!

That's right, the book doesn't even cover if while, but it does start moving Pointers out, even though it's just a brief introduction to...
I woke up to this directory too.

So, according to the progress of the book, today's first talk about Pointers.
But, today's Friday, you know, just take a dip, because I'm afraid I'll forget it on Monday.

1. The enumeration

So what is an enumeration? (xiao ruo: wait ~! Where's the pointer?)
Most high-level languages have enumerations, so there's nothing to tell you about them.
Here's how it works:


//Define < br / > enum MonsterType {
   enMonster_nor,
   enMonster_hard,
}
//Declare an enumeration variable
MonsterType mType;
//Use the < br / > if(mType == enMonster_nor) {
   log(" Go to dead ~ ! ");
}
else if(type == enMonster_hard) {
   log(" Oh oh, hello, this is my salary this month, have given you oh, I admire you most, you are the most in my life ....(1 Word omitted )");
}

2. What is a pointer

Since this is not a real C++ tutorial, it's just a supplement, so I won't talk about the basic concepts.
A pointer is something that points to a memory space, yes, it just points to a memory space, it's not a memory space.
 
Okay, no nagging.

Declare Pointers

Declaring pointer variables is a simple matter, as follows:


int num = 10;
int *p = #

Yes, even our old plain variables had memory addresses, and you can get the content address using the ampersand.
Then, our pointer variable p is dedicated to the memory address.
When you declare a variable, you put a * sign on it and it's a pointer variable.

Remember, this is the declaration that if you don't learn Pointers, you're going to get confused.

4. Use Pointers

How do I use Pointers? Very simple, the following code:


int num = 10;
    int *p = #
    *p = 20;
    std::cout << *p << "n";
    std::cout << num << "n";

And then finally, both p and num are 20.
P is the pointer variable that holds the memory address of the num variable.
And *p is the value above this memory address, which is the value of our variable num.
Therefore, *p = 20 means that the value on the memory address is changed to 20.

Here's the key:

1. Assume the memory address of num 0xffffff, where the storage 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 stored content at this address is the number 10
3.*p represents the contents stored above the memory address pointed to by p, which is the number 10. In other words, *p has a value of 10
4. Point to *p = 20, which is equivalent to changing the contents above the memory address pointed to by p to 20
5. Since the memory address of num is the address pointed to by p, the value of num stored in memory has changed
6. Finally, the value of num will also become 20

See? (xiao ruo: say it again in Chinese)
Well, to repeat it in Chinese, that is:

Since the memory address to which p refers is the memory address to which num is located, both num and *p can be used to change the contents of this address.
In a word, at this point *p equals num, and you can do anything (assign, add, subtract, etc.)
Xiao ruo: how nice to speak Chinese earlier! Do you have to show off your half-tone Japanese? !).

5. Pointer out

Sometimes, we can declare Pointers like this:


int *num;
//But you mustn't use
that way *num = 100;

At first glance, this seems like a normal use, just like I said.
However, notice that the pointer num was not assigned when it was declared, that is, the pointer does not now know where to point.
So, using a pointer like this, which I don't know where, is likely to break 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 doesn't know where it's pointing, it's probably pointing where it used to point.
What if the needle used to point down from our waists?
 
In case we use this pointer to perform "cut the pointer to the point, all cut."
Yeah, I thought it was hair, but it turned out to be! Cut it from the waist down!
What do you do for the rest of your life?

6. Over

Ok, that's all for today ~ see you next time


Related articles: