The difference between a constant pointer and a pointer constant (reprint)

Source: Internet
Author: User
Tags strcmp

Three nouns, though very raozui, are very accurate. The semantic analysis of Chinese words can easily separate three concept areas.

one) constant pointer.

A constant is an adjective, and a pointer is a noun, a biased structure phrase centered on the pointer. In this way, the constant pointer is essentially a pointer, and the constant modifies it to indicate that the pointer is a pointer to a constant (a variable).

The object that the pointer is pointing to is a constant, then the object cannot be changed.

In C + +, the constant pointer is declared like this:

1) const int *p;

2 int const *P;

The use of constant pointers note that the object to which the pointer is pointing cannot be modified by this pointer, but can still be modified through the original declaration, that is, the constant pointer can be assigned to the address of the variable, called a constant pointer, which restricts the value of modifying the variable through this pointer. For example:

int a = 5;

const int B = 8;

const int *C = &a;//This is legal and illegal is the use of C

*c = 6; Illegal, but you can modify the value of the object that C points to: a = 6;

const int *D = &b; b is a constant, D can point to b,d the address assigned to B is legal

Careful friends should notice the declarations of these functions when using string-handling functions. Their arguments are generally declared as constant pointers. For example, the declaration of a string comparison function is like this:

int strcmp (const char *STR1, const char *STR2);

However, this function can receive a very literal string. For example, this procedure:

Char *str1, *STR2;

str1 = "abcde1234";

str2 = "BCDE";

if (strcmp (str1, str2) = = 0)


printf ("str1 equals str2.");


The contents of str1 and str2 are clearly subject to change, for example, you can use "str1[0" = x; Such statements change the contents of str1 from "abcde1234" to "xbcde1234". Because the parameter declaration of a function is in the form of a constant pointer, it guarantees that the constant is not changed within the function. That is, the operation of changes to the contents of str1 and str2 is not allowed inside the function . (For the current application, I think the set constant pointer is for the function parameter declaration prepared, otherwise I really do not know where to use, hehe.) )

Although the object that the constant pointer points to cannot change, because the constant pointer is a variable, the constant pointer can be unassigned without being assigned an initial value. For example:

const int a = 12;

const int b = 15;

const int *C = &a; To simplify the code, many people are accustomed to assigning initial values

const int *D;

D = &a; Of course it's okay.

c = &b; Although C has been given an initial value, it can still point to another variable

The feature is that the const position is on the left side of the pointer declaration operator *. As long as the const is on the left side of the *, whether it is on the left or right side of the type name, a pointer to a constant is declared, called a constant pointer.

You can think that, * the left side is a constant, and the object to which the pointer points is a constant.

Two) pointer constants

The pointer is an adjective, and a constant is a noun. This is a partial-positive structure phrase centered on a constant. The essence of a pointer constant, then, is a constant, and it is decorated with a pointer, so the value of the constant should be a pointer.

the value of a pointer constant is a pointer, which cannot be assigned because it is a constant.

In C + +, pointer constants are declared like this:

int A;

int *const B = &a; Const is placed on the right side of the pointer declaration operator

as long as the const is on the right side of the pointer declaration operator, the declared object is a constant, and its contents are a pointer, which is an address. The above declaration can be read in this way, declaring a constant B, whose value is the address of variable a (the address of variable A, not the pointer to variable a).

Because the pointer constant is a constant, be sure to assign an initial value to it when you declare it. Once the value is assigned, the constant can no longer point to another address.

Although the value of a pointer constant cannot be changed, the object it points to is mutable because we do not limit the object it points to as a constant.

So, there is this procedure:

Char *a = "abcde1234";

Char *b = "BCDE";

char *const C = &a;

The following actions are possible.

A[0] = ' x '; We do not limit A to a constant pointer (a pointer to a constant)


*c[0] = ' x '//consistent with the operation above

three pointer constants pointing to constants

As implies, the pointer constant to a constant is a constant, and the object it points to is also a constant.

Because it is a pointer constant, the object it points to is of course a pointer object, and it points to a constant, indicating that the object it points to cannot be changed.

In C + +, this declares:

const int a = 25;

const INT * Const B = &a;

See, the pointer declaration operator has a const on the left, stating that a pointer to a constant is declared. Again, the pointer declaration operator has a const on the right, stating that a pointer constant is declared. are locked before and after, the object cannot be changed, and the pointer constant itself cannot be changed. Savor, believe you can get its way, the following is not to repeat.

Use an example as a summary. Although character pointers are the same as the nature of other pointers, they are often difficult to understand because character pointers are used to represent strings. Here is an example of a character pointer.

Char *a = "abcde1234";

const char *b = "BCDE"; b is a pointer variable that points to a constant string

char *const C = &a; C is a constant that points to a character pointer variable

const char *const d = &b; D is a pointer constant pointing to the constants of the word

Here's the problem.

1) Q: Because A is a variable, a can be assigned to another value, such as "12345ABC". So c points to a, and when a changes, what does C point to?

answer: Still point to "abcde1234". Although a can point to another string, C still points to "abcde1234", which is the object that a starts to point to.

2) Q: A is a variable that can change the content of a. Then when the "a[0] = ' x ' is executed"; After that, C will do.

A: C of course also points to the character of the initial point. However, this character has become an ' x '.

3) Q: b is a pointer variable that points to a constant, and when B points to another string, D.

A: D still point to the original B string.

4) Q: B can be changed, b point to the character can not be changed, that is, b[0] can not be assigned a value, but b[1 could be re assigned.

A : In principle, B points to the character is a constant, and does not restrict the next character, should be able to be assigned value. But because you use a string for the initial assignment, and the compiler is statically compiled, C + + programs handle B as a string pointer, so the compilation cannot pass when the next character is assigned.

other questions, please add.

I made up such a formula, remember, it should be easy:

Const (*) on the left, I am the pointer variable pointing constant;

Const (*) on the right, I am the pointer constant pointing to the variable;

Const (*) on both sides, I am the pointer constant pointing constant;

The pointer variable can be pointed, and the pointer constant cannot be changed.

If all become constant, locked, I can't turn, you don't want to change.

Contact Us

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: 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.