Pointer constant, constant pointer
Sometimes, if you are not careful, you will confuse the two. Even if you look at the data again, you will forget it again after a period of time. So today, I will use the actual code to see what the difference is.
First, what does a constant mean in C/C++?
The keyword for ** constants is const, that is, cannot be changed. ** At compile time, the compiler will prompt you if it finds that a constant has been modified. Because of this, constants must be initialized when they are declared and cannot be changed afterwards, as shown below:
If not initialized:
Try to change:
Ok, now we know what a constant means: a value that cannot be changed after initialization. One more trivia: What are Pointers?
Simply put, a pointer is a box containing a key that can be used to open a corresponding safe to retrieve items.
Box = pointer, which may be 4/8 bytes in size, depending on the system bits 32/64;
Key = memory address, depending on the system bit 32/64, this key size is also 4/8 bytes;
So, what is a room?
Safe = memory fragments, using the key to hide the highest mystery – memory address, access the corresponding memory address of memory fragments (room), take out the treasure!
The box, the key, the safe, which of these three can be changed?
Answer: keys and safes can be changed, but boxes cannot.
Why not a box?
Because the box is the address of the pointer object itself, it is already determined when the pointer is declared (which memory location to “put” the pointer box in). As shown in the figure below, the position of P1 pointer box has been determined since it was born, and there is no rearrangement problem:
Let’s get this straight. The only thing we can change is the key in the box and the safe to which it corresponds.
How do I change the key?
— Easy, just open the box and change the key to something else, like:
In the box P1 above, the key used to open the safe of “1” has been replaced by the key used to open the safe of “2”.
PS: new remember delete, example then write will run out of memory;
How do YOU change the safe?
— Before we answer, we need to note that the key and safe come with each other! If the key is changed, the safe is opened differently, and the relationship between the safe and the key itself cannot be changed (in general).
So, when we say “change the safe”, we really mean change the contents of the safe, open the safe, change the contents of the box, see below:
Above, open the safe and change the “one-finger Zen” into “two-finger Zen”!
Okay, so back to business, what are pointer constants and constant Pointers?
Pointer constants are the Pointers themselves are constants.
This is nonsense, just said that the pointer box is not changeable! ?
The pointer itself is a constant. The key stored inside the pointer cannot be changed. Do you think that the same box plus the same key in the box soon equals the same pointer?
A constant pointer is a constant to which a pointer points.
Can this statement be understood?
Only the key and the safe can be changed. The constant pointer is the key corresponding to the safe, its contents cannot change! The safe deposit at the beginning is “one finger Zen”, until death can only be “one finger Zen”!
Take a look at the following two examples and think about which is a pointer constant and which is a constant pointer.
The key, the box, the safe should be simple.
Figure 1 is a constant pointer that cannot change the contents of the safe, but – can change the key in the box;
Figure 2 is a pointer constant that cannot change the key in the box, but – can change the contents of the safe;
How are these two things grammatically defined?
Const * (const *); const * (const *); const * (const *); const * (const *);
int* const p1=new int(1);
Copy the code
Pointer constants above and constant Pointers below:
int const *p1=new int(1);
const int *p1=new int(1);
Copy the code
To the end.