The const and pointer combinations in C + +

Source: Internet
Author: User

In C + +, the const modifier pointer has the following three cases

(1) Pointer constant: A pointer to a constant

const int *P or int const *P

Const in the * before,, can understand its function, because const in the * before, so P point to this int variable for *p is const, that is, cannot change the value of the variable by *p, but the variable itself can arbitrarily change its value. Alternatively, you can change the direction of P.

Cases:

int x=2;

int y=3;

const INT *p=&x;

*p=4; Error, cannot change the value of a variable by *p

x=4; Yes, x is not a constant, you can change your own value

p=&y; Yes, the pointer p itself is not a constant and can change the direction of P.

(2) Constant pointer:

int * Const P,CONST at the back of *, that is, the pointer p itself is a constant, can not change the direction of P. However, you can change the value of the pointed variable by *p.

Cases:

int x=2;

int y=3;

int * Const p=&x; Note: Because pointer P is a constant, it should be initialized at the time of declaration.

*p=4; OK

x=4; Yes, x is not a constant, you can change your own value

p=&y; Error, the pointer q itself is a constant and cannot change its point.

(3) constant pointer to constant: this is the combination of the first two, the function is the combination of the former, that is, can not change the direction of P, nor through the *p to change the value of the variable pointed to.

const INT * const p or int const *P

Cases:

int x=2;

int y=3;

const INT * Const pq=&x;

*pq=4; No, it is not possible to change the meaning of the indicated variable by *PQ.

x=4; Yes, x is not a constant, you can change your own value

p=&y; Error, the pointer PQ itself is a constant and cannot change its point.

Summary: As can be seen from the above, in either case, the variable itself can change its own value, unless the variable itself is declared as a constant (const int x=2;). The key to distinguish between three kinds of situations is to see if the const is before the type or before the pointer, before the type, cannot change the value of the variable by *p, cannot change the pointer's point before the pointer, and initializes it at the time of declaration.

The const and pointer combinations in C + +

Related Article

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.