Differences between constant pointers and pointer Constants

Source: Internet
Author: User

[Transfer] http://www.cnblogs.com/witty/archive/2012/04/06/2435311.html

Constant pointer constantPointer constant pointing to a constant

Although the three terms are very vague, they are very accurate. With the semantic analysis of Chinese words, the three concepts can be easily separated.

1) constant pointer

Constants are adjectives, pointers are nouns, and pointer-centered partial positive structure phrases. In this case, a constant pointer is essentially a pointer, and a constant modifies it, indicating that this pointer is a pointer (variable) pointing to a constant ).

If the pointer points to a constant, the object cannot be changed.

In C/C ++, constant pointers are declared as follows:

1) const int * P;

2) int const * P;

Note that the object pointed to by the pointer cannot be modified through this pointer, but it can still be modified through the original declaration. That is to say, the constant pointer can be assigned the variable address, the constant pointer restricts the modification of variable values through this pointer. For example:

Int A = 5;

Const int B = 8;

Const int * c = & A; // This is legal and the use of C is invalid.

* C = 6; // invalid, but the value of the object pointed to by C can be modified as follows: a = 6;

Const int * D = & B; // B is a constant. D can point to B. it is legal to assign a value to B.

When using string processing functions, careful friends should pay attention to the declarations of these functions. Their parameters are generally declared as constant pointers. For example, the declaration of the string comparison function is as follows:

Int strcmp (const char * str1, const char * str2 );

However, this function can receive a very large number of strings. For example, this program:

Char * str1, * str2;

Str1 = "abcde1234 ";

Str2 = "bcde ";

If (strcmp (str1, str2) = 0)

{

Printf ("str1 equals str2 .");

}

The content of str1 and str2 can obviously be changed. For example, you can use a statement like "str1 [0] = x;" to change the content of str1 from "abcde1234" to "xbcde1234 ". Because the parameter declaration of a function uses a constant pointer, it ensures that the constant is not changed within the function. That is to say, the changes to the content of str1 and str2 are not allowed in the function. (As far as the current application is concerned, I think setting constant pointers declares quasi-standby for function parameters. Otherwise, I really don't know where to use them !)

Although the object to which the constant Pointer Points cannot be changed, because the constant pointer is a variable, the constant pointer can be reassigned 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 used to assigning initial values.

Const int * D;

D = & A; // of course this is acceptable

C = & B; // although C has been assigned an initial value, it can still point to another variable

The const is located on the left side of the pointer declaration Operator. As long as the const is on the left side of *, whether it is on the left or right side of the type name, it declares a pointer to a constant, called a constant pointer.

In this case, * the left side is a constant, and the pointer points to a constant.

 

2) pointer Constants

A pointer is an adjective and a constant is a noun. This is a positive structure phrase centered on constants. Then, the essence of a pointer constant is a constant, and it is modified with a pointer, it means that the value of this constant should be a pointer.

The value of a pointer constant is a pointer. This value cannot be assigned because it is a constant.

In C/C ++, the pointer constant is declared as follows:

Int;

Int * const B = & A; // place const on the right of the pointer declaration Operator

As long as the const is on the right of the pointer declaration Operator, it indicates that the declared object is a constant, and its content is a pointer, that is, an address. The above statement can be read in this way. It declares a constant B whose value is the address of variable A (the address of variable A, isn't it a pointer to variable ).

Because a pointer constant is a constant, you must assign it an initial value when declaring it. Once assigned, this constant can no longer point to another address.

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

Therefore, there is such a program:

Char * A = "abcde1234 ";

Char * B = "bcde ";

Char * const c = &;

The following operations are acceptable.

A [0] = 'X'; // we do not limit A to a constant pointer (pointer to a constant)

Or

* C [0] = 'X' // consistent with the preceding operation

 

3) pointer constant pointing to a constant

The pointer constant pointing 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/C ++, the statement is as follows:

Const int A = 25;

Const int * const B = &;

Look, there is a const on the left of the pointer declaration Operator, indicating that it declares a pointer pointing to a constant. Again, there is a const on the right of the pointer declaration Operator, indicating that it declares a pointer constant. The front and back are locked, so the object to be pointed to cannot be changed, and the pointer constant itself cannot be changed. I believe you can do it. I will not go into details below.

Use an example as a summary. Although character pointers are essentially the same as other pointers, character pointers are often used to represent strings and are hard to understand. The following uses a character pointer for example.

Char * A = "abcde1234 ";

Const char * B = "bcde"; // B is a pointer variable pointing to a constant string

Char * const c = & A; // C is a constant pointing to a character pointer variable

Const char * const d = & B; // D is a pointer constant pointing to a character constant

The problem arises.

1) Q: because a is a variable, A can be assigned another value, such as "12345abc ". So C points to a. When a changes, what does C point?

A: It still points to "abcde1234 ". Although a can point to other strings, C still points to "abcde1234", that is, the object that a starts to point.

2) Q: A is a variable and can change the content of. After "A [0] = 'X';" is executed, what will happen to C?

A: Of course, C also points to the characters initially pointed to by. However, this character has become 'x '.

3) Q: B is a pointer variable pointing to a constant. How about D when B points to another string?

A: d still points to B's initial string.

4) Q: B can change, but B cannot change the characters it points to. That is to say, B [0] cannot be re-assigned, But B [1] can be re-assigned?

A: In principle, B points to a constant and has no limit on the next character. It should be assigned a value. However, because you use a string for initial assignment and the compiler is statically compiled, the C/C ++ program treats B as a string pointer. Therefore, when a value is assigned to the next character, compilation fails.

For other questions, please add.

 

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