# Differences between pointer constants and constant pointers

Source: Internet
Author: User

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 constant pointer, changed the pointing address
8
9 printf ("% s n", * ptr1 );
10}
11
12
13 // compilation Error 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;

Memory skills:
For distinction between const int * pa and int * const Pa,
In the former, const directly modifies * (INT is not considered, because the type is not affected here), which indicates that * (unreferencing) This behavior is constant, that is, "the value of the object to which it points cannot be changed by resolving the reference", that is, the pointer to a constant.
In the latter, const directly modifies Pa, indicating that the value of PA itself is constant, that is, a constant pointer.

Or you can remember it like this:
Const int A; // const constant
Const int * A; // pointer to a constant
Int * const A = & N; // constant pointer
Write the preceding three lines in order on the paper, and remember their comment length: short-long-short,
They correspond to three types of constants: const constant, pointer to constant, and constant pointer.

I personally think that the above memory method is better to remember than the underlined left/right method recommended in article 21 of effective ++.

The last example is often used together with the iterator.
If you want the iterator to point to something immutable, you need const_iterator. Example:

After reading this article, I feel like a good post is normal.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.