In C/C ++ learning, some people often cannot understand the concepts of "constant Pointer" and "pointer constant. In fact, the data on the address pointed to by the "constant Pointer" is a constant, while the address pointed to by the "pointer constant" is a constant, and the data on the address can be changed.
First, I would like to tell you a small rule, that is, two words connected like this. The first one is usually the modifier, and the Central Word is the last word, just like the constant pointer and pointer constant here.
Constant pointer, expressed as "a constant Pointer". It should be a pointer first. A constant pointer is a pointer to a constant. The const keyword appears on the left of *, indicating that the content of the address pointed to by the pointer cannot be modified, but the pointer itself is variable.
A pointer constant, expressed as a constant of a pointer. It should first be a constant. Pointer constant. The pointer itself is a constant. The const keyword appears on the right side of *, indicating that the pointer itself is unchangeable, but the content of the address to which it points can be modified.
Let's further explain in detail what kind of pointer is a constant pointer? It is a pointer to a constant, that is, we define a constant, such as const int a = 7; then we can define a constant pointer to point to it const int * p = & a; it can also be divided into two steps, namely const int * p; p = &; so what does it do? First, let's talk about the attributes of constants, because our pointers point to constants. The difference between constants and variables is that we cannot perform operations on their content. Specifically, we modify them, what is our pointer, its content is itself an address, set the constant pointer to a constant, in order to prevent the pointer misoperation during the write process, such as modifying the constant, if we modify the space pointed to by the constant pointer, the compilation system will prompt us with an error message. To sum up, a constant pointer is a pointer to a constant. The content of the address pointed to by the pointer cannot be modified.
Let's talk about the pointer constant. It is a constant first, and then a pointer. A constant cannot be modified. The pointer content is actually an address. A pointer constant is a constant that cannot be modified in the content, that is, a pointer that cannot be modified in the content. What is the pointer content? The pointer content is the address. In the end, you cannot modify the address pointed to by the pointer. It can only point to the address at the beginning of initialization, it cannot point to other places, just like the array name of an array. It is a fixed pointer and cannot be moved. For example, you can use p ++; the system prompts an error. But it just cannot modify the point it points to, but the content in the point can be replaced, which is totally different from the constant pointer mentioned above. To sum up, a pointer constant is a constant of a pointer, which cannot change the address, but can be modified. By the way, I forgot to talk about how to use it. For example, int a; int * const p = & a; you can also separate int a; int * const p; p = &;
Of course, you can also define a pointer constant pointing to a constant, so that you can combine the above two values to indicate the following
Const int a = 7; const int * const p = &;
The following are some simple examples to illustrate their differences:
First
1 void main (){
2 char * str1 = {"Hello "};
3 char * str2 = {"Hello World "};
4 char * const ptr1 = str1;
5 // pointer constant -- the pointer itself is a constant, and the address to which it points cannot be changed, but the content corresponding to the address to which it points can be changed
6
7 ptr1 = str2; // error because this is a pointer constant, changed the pointing address
8
9 printf ("% s n", * ptr1 );
10}
11
12
13 // compilation error C3892: 'ptr1': you cannot assign to a variable that is const
14
Second
1 void main (){
2 char * str1 = {"Hello "};
3 char * str2 = {"Hello World "};
4 char * const ptr1 = str1;
5 // pointer constant -- the pointer itself is a constant, and the address to which it points cannot be changed, but the content corresponding to the address to which it points can be changed
6
7 * ptr1 = 'a'; // It is correct because the content of the address to which it points can be changed.
8
9 printf ("% c n", * ptr1 );
10}
11
12 // output
13
Third
1 void main (){
2 char * str1 = {"Hello "};
3 char * str2 = {"Hello World "};
4 const char * ptr1 = str1;
5 // constant pointer-pointing to a String constant. The content of the string to which it points cannot be changed, but the address to which it points can be changed.
6
7 ptr1 = str2; // It is correct because the address to which it points can be changed.
8
9 printf ("% s n", ptr1 );
10}
11
12 // output Hello World
Fourth
1 void main (){
2 char * str1 = {"Hello "};
3 char * str2 = {"Hello World "};
4 const char * ptr1 = str2;
5 // constant pointer-pointing to a String constant. The content of the string to which it points cannot be changed, but the address to which it points can be changed.
6
7 ptr1 = 'a'; // error because the address to be pointed to cannot be changed because the content
8
9 printf ("% c n", ptr1 );
10}
11
12
13 // compilation error C2440: '=': cannot convert from 'Char 'to 'const char *'
14
I believe that from the above four simple examples, we can see that they are different from each other. Here, please note that:
Pointer constant Declaration: const is placed between * and pointer name Type * const pointer;
Statement of the constant pointer: place the const on the left.