Let's go back to what is a pointer? What is a constant? A pointer is a special variable in which the contents are stored as memory addresses. A constant is the amount of content stored inside it that cannot be changed. Having understood these two concepts, we are now officially entering pointer constants and constant pointers.
1. The concept of pointer constants and constant pointers
The pointer constant is that the pointer itself is a constant, in other words, the content stored in the pointer (the memory address) is constant and cannot be changed. However, the contents of the memory address can be changed by the pointer.
A constant pointer is a pointer to a constant, in other words, the pointer is a constant, the content it points to cannot be changed, and the content it points to cannot be modified by a pointer. However, the pointer itself is not a constant, and its own value can be changed to point to another constant.
2. Pointer constants and constant pointer declarations
Declaration of pointer constants: data type * Const pointer variable.
Declaration of a constant pointer: data type const * Pointer variable or const data type * pointer variable.
Declaration of constant pointer constants: Data type const * Const pointer variable or const data type * Const pointer variable.
3. Pointer constants and the use of constant pointers
3.1 Examples of pointer constants
/* Pointer Constant example *
/int a,b;
int * const P;
p = &a;//correct
p = &b;//error
*p = 20;//correct
You must assign an initial value when you declare a pointer constant. Using pointer constants can increase the reliability and execution efficiency of your code.
3.2 Example of a constant pointer
/* The example of the constant pointer *
/int a,b;
int const *P;
p = &a;//correct
p = &b;//correct
*p = 20;//Error
A tip about distinguishing pointer constants: the content after the const is not modifiable. For example, the pointer constant int * CONST P = &a; means that the contents of the pointer p cannot be modified; the constant pointer int const *P = &a; Indicates that the content pointed to by the pointer P cannot be modified.
In this, what are pointer constants and constant pointers, and the difference between the two, should be clear and bright.