When reviewing the C++ foundation, it is confused by constant pointer and pointer constant. This blog compares the two differences.
Original address: https://www.cnblogs.com/lizhenghn/p/3630405.html
———————-I am the text dividing line.
In the process of C++ learning, each person inevitably uses pointers and is more or less exposed to constant pointers or pointer constants, but the concept of the two is still very easy to confuse.
This is a simple description of the difference between pointer constants and constant pointers.
It is also called the constant pointer, which can be understood asA constant pointer, which is a pointer, points to a constant, which is the value (address) of the pointer, rather than the value of the address.
1.The pointer to the constant pointer cannot be modified by this pointer, but it can still be modified by the original declaration.
2.A constant pointer can be assigned to a variable’s address, which is called a constant pointer.It is a restriction to modify the value of variables through this pointer.
3.The pointer can also point to another place, because the pointer itself is only a variable and can point to any address.
int const* p; const int* p;
Nature is a constant and modifies it with a pointer.The value of pointer constant is a pointer. Because this value is a constant, it cannot be assigned.
1.It’s a constant!
2.The address saved by the pointer can be changed, but the value pointed by the pointer can not be changed.
3.The pointer itself is a constant,The address that is directed can not change, but the content corresponding to the address can be changed.
int* const p;
Constant pointer to constant quantity
A pointer constant to a constant is a constant and the object it points to is also a constant.
1.A pointer constant refers to a pointer object.
2.The pointer object it refers to is a constant, that is, the object it points to can not change.
const int* const p;
How do we distinguish these categories? The two const must be a constant pointer to the constant. It is easy to understand. It is mainly how to distinguish between constant pointer and pointer constant.
One way is to see * And the order of const，such as
int const* p; //const * Constant pointer
const int* p; //const * Constant pointer
int* const p; //* const Pointer constant
The other way isSee who const is near, from right to left，such as
int const* p; //const*p is modified, that is, the content of *p can not be changed by P, but p is not const, P can be modified, *p can not be modified.
const int* p; //Ditto
int* const p; //constP is modified, P is pointer, p address can not be modified, P can not be modified, but *p can be modified.
Look at the code
//-------Constant pointer - - ---- const int *p1 = &a; a = 300; //OK,It is still possible to modify the value by the original declaration.//*p1 = 56; //Error,*p1It is const int, and can not be modified, that is, constant pointer can not modify its address. p1 = &b; //OK,The pointer can also point elsewhere because the pointer is just a variable and can be pointed at random.//-------Pointer constant -- - ----// int* const p2 = &a; a = 500; //OK,It is still possible to modify the value by the original declaration. *p2 = 400; //OK,The pointer is a constant, and the address to be changed can not be changed, but the content corresponding to the address can be changed.//p2 = &b; //Error,Because P2 is const pointer, it can not change the content of P2.//-------A constant pointer to a constant -- - ----// const int* const p3 = &a; //*p3 = 1; //Error //p3 = &b; //Error a = 5000; //OK,It is still possible to modify the value by the original declaration
In practical applications,The constant pointer is more than the constant of the pointer，such asConstant pointer is often used in function reference to avoid modifying contents internally.
size_t strlen(const char* src); //Constant pointer, the value of SRC can not be changed.
char a = “hello”;
char b = “world”;
size_t a1 = strlen(a);
size_t b1 = strlen(b);
Although a and B can be modified, it is guaranteed that the contents of a and B will not be modified inside the strlen function.
Now that we talk about pointer, let’s talk about empty pointer and wild pointer.
Empty pointerIt is to save the empty pointer. When you use the pointer, you must first determine whether it is empty pointer. Many problems are caused by this step.
Wild pointerIt is after the delete drops the pointer, not 0, causing the pointer to point to a memory address at will, and if continued, it will cause unpredictable memory errors.
In addition, the misuse of pointers can easily cause BUG or memory leaks.
Look at the code
//-------Empty pointers - ----// int *p4 = NULL; //printf("%d",*p4); //To run Error, you must first determine whether the pointer is empty or not.//-------Wild pointers (suspended, lost pointers) - ----// int *p5 = new int(5); delete p5; p5 = NULL; //There must be this step printf("%d",*p5); //Hide bug, delete must set 0 after dropping pointer, otherwise pointer pointing to uncontrollable position, operation can cause system to hang up.//-------The memory leak of the pointer - - ----// int *p6 = new int(6); p6 = new int(7); //p6The memory originally pointed to has not been released yet, and P6 is pointing elsewhere. The memory of the original new can not be accessed, nor can it be delete, resulting in memory leak.