C-language pointer constants and constant pointers

Source: Internet
Author: User
Tags strcmp

The three nouns, though very raozui, are very accurate. The semantic analysis of Chinese language can easily separate the three conceptual areas.

A) constant pointer.

Constants are adjectives, pointers are nouns, and pointers are the center of a partial positive structure phrase. In this way, a constant pointer is essentially a pointer, and a constant modifies it, indicating that the pointer is a pointer to a constant (variable).

The object pointed to by the pointer is constant, and the object cannot be changed.

A constant pointer is declared like this in C + + +:

1) const int *p;

2) int const *P;

Use of constant pointers note that the object pointed to by the pointer can not be modified by this pointer, but can still be modified by the original declaration, that is, the constant pointer can be assigned to the address of the variable, is called a constant pointer, is limited by the pointer to modify the value of the variable. 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, and D can point to b,d the address that is assigned to B is legal

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

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

But 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 obviously subject to change, such as "str1[0" = x; " Such statements change the contents of str1 from "abcde1234" to "xbcde1234". Since the function's parameter declaration is in the form of a constant pointer, it guarantees that the constant is not changed inside the function. That is, the operation of content changes to 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 it, hehe!) )

Although the object pointed to by the constant pointer cannot be changed, because the constant pointer is a variable, the constant pointer can be assigned no initial value and can be re-assigned. 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 possible.

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

The characteristic is that the position of the const 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, it declares a pointer to a constant, called a constant pointer.

As you can imagine, the left side is a constant, and the pointer to the object is constant.

II) Pointer constants

A pointer is an adjective, a constant is a noun. This is a partial positive structure phrase centered on a constant. Then, the essence of the pointer constant is a constant, and the pointer decorates it, then the value of the constant should be a pointer.

The value of a pointer constant is a pointer, and this value cannot be assigned because it is a constant.

Pointer constants are declared in C + + in this way:

int A;

int *const B = &a; The const is placed to the right of the pointer declaration operator

As long as the const is on the right side of the pointer declaration operator, it indicates that the declared object is a constant, and that its contents are a pointer, that is, an address. The above declaration can read this, 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, it must be assigned an initial value at the time of declaration. 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 mutable because we do not restrict the object it points to is a constant.

Therefore, there is a procedure:

Char *a = "abcde1234";

Char *b = "BCDE";

char *const C = &a;

The following actions are possible.

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

Or

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

Three) pointer constants to constants

As implies, a 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 change.

In C/A + +, this declares:

const int a = 25;

const INT * Const B = &a;

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

Use an example as a summary. Although the character pointer is the same as the nature of other pointers, it is often difficult to understand because character pointers are commonly used to represent strings. Here's an example with 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 pointing to a character pointer variable

const char *const d = &b; D is a pointer constant pointing to a character constant

Here's the problem.

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

Answer: Still point to "abcde1234". Although a can point to a different string, C still points to "abcde1234", which is the object to which a starts pointing.

2) Q: A is a variable that can change the contents of a. Then when executed "a[0" = ' x '; " What will happen to C?

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

3) Q: b is a pointer variable pointing to a constant, and when B points to another string, how about d?

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

4) Q: B can change, B point character can not be changed, that is b[0] can not be re-assigned, but B[1] could be re-assigned?

A: In principle, the character B is a constant, and there is no limit to the next character, it should be assignable. However, because you use a string for the initial assignment, and the compiler is statically compiled, the C + + program treats B as a string pointer, so the compilation does not pass when the next character is assigned.

Other questions, welcome additions.

I made up such a formula, remember, should not be difficult:

Const (*) left, I am pointer variable point constant;

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

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

Pointer variable can be changed to point, pointer constant cannot turn!

If all becomes constant, lock dead, I can't turn, you also don't want to change!

C-language pointer constants and constant pointers

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.