Int * P, cons int * P, int const * P, int * const P, const int * const P, int const * const P

Source: Internet
Author: User
Zookeeper
  1. Analysis of several cases with the const keyword

Const modified code

Meaning (features)

Equivalence

Int * P = & num;

1. You can read yourself

2. You can change yourself through * P.

3. You can view other users through P = & Data

Maximum Permissions

Cons int * P = & num;

1. Putting const on the left means that it points to a constant, which cannot be modified,

2. p = & data; (the address can be modified)

3. * P = 30; (this time is incorrect)

The two are equivalent (Application: View others' accounts)

Int const * P = & num;

(Equivalent to the above) the limit can only be read, cannot be modified, and the address can be moved

Int * const P = & num;

You cannot modify your own address, but you can use * P to modify your own value.

 

Const int * const P = & num;

You cannot modify the address to view other values or modify your own values. You can only view your own values.

1. You can view yourself

2. Do not change yourself

3. Do not look at others

Int const * const P = & num;

Only your account can be read and not written.

 

  1. Values can only be assigned during initialization (const limits that variables cannot be modified randomly)

  2. Const Summary

On the left side of '*', const indicates that I am pointing to a constant.

On the right side of '*', const indicates a constant of the pointer.

You cannot change the above P = & data; to * P = 3. As follows:

 

  1. Int const * P

  1. Int * const P; you cannot modify your own address, but you can use * P to modify your own value.

You can modify it in * P mode.

6. Const int * const P = & data; in this way, you can only view your own data, neither modify others nor change yourself.

You can only view yourself:

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.