2nd. Variables and basic Types 3---Underlying const and top-level const

Source: Internet
Author: User

and the English version of the pair:

As we ' ve seen, a pointer is an object and can point to a different object. As a result,
We can talk independently on whether a pointer is const and whether the objects
To which it can point is const. We use the term top-level const to indicate the
Pointer itself is a const. When a pointer can point-to-a const object, we refer to
That const as a low-level const.

-----------------------------------------------------------------

The English language is obvious: the top-level const (TOP-LEVEL-CONST)-----The pointer itself is a constant, and the underlying const (low-level const) pointer refers to the object that is a constant.

1 const Qualifier and pointer2         Const int* p;//Const on the left, indicates that *p is constant and cannot be changed (via *p cannot change what the pointer points to)3                      //but the pointer p or the variable, want to change all can. This is called the underlying const.4 Example:5         intb = A;6         Const int*p;7p = &b;8         //* p = 200; //Error *p is a constant and can no longer be assigned to a constant value9 Ten          One         int*Constp = &b;//must be initialized at the same time as the Declaration, const on the right side of the *, indicating that P is constant, p points to the address A                           //is immutable, so when the address of B is assigned to it, an error is given. This is the so-called top-level const - Example: -         intb = -; the         intc = A; -         int*Constp = &b;//must be initialized at the same time as the Declaration, const on the right side of the *, indicating that P is constant, p points to the address -                           //is immutable, so when assigning the address of B to it, an error is given . -         //p = &c; //Error p is constant, top-level const +  -  +         Const int*ConstP//This is equivalent to the mixture of the above two cases, p is a constant, A                           //Therefore, the Test2 address can not be assigned to P, while *p is also constant, so the contents of *p cannot be changed; at Example: -         intTest1 =Ten; -         intTest2 = -; -         Const int*Constp = &test1; -p = &test2;//Error,p is a constant, so the address of test2 cannot be assigned to P; -*p = -;//Error,*p is constant, so the contents of *p cannot be changed;

2nd. Variables and basic Types 3---Underlying const and top-level const

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.