Constant pointer and pointer constant

Source: Internet
Author: User

Constant pointer and pointer constant

# Include
 
  
Using namespace std; int main () {int a = 5, B = 10, d = 20; int * const c = & a; * c = a; cout <
  
   

Output: (No screenshots will be taken in Ubuntu)

Try it by yourself. You can see that the same address is output, and there are 5, 10;

The most important thing to remember is that cosnt modifies the thing before it, that is, int *, and int * is a pointer, so it is understood that the pointer is modified as a constant (so the output address of the program remains unchanged, but its memory value can be changed)

Assign values when defining them. Otherwise, an error occurs,

C = & B; no, because c is a constant !!!

# Include
    
     
Using namespace std; int main () {int a = 5, B = 10, d = 20; int const * c = & a; c = & d; cout <
     
      

It's easy to understand the program to modify the previous content of const.

The output result is different addresses and values.

Because it is not a constant pointer, the address of c can be changed at will;

But cannot * c = 50; because the value in c memory cannot be changed;

Although I know the literal meaning of constant pointers and pointer constants, I forget it in a few days. Anyway, knowing how to use it is the most important thing.

The most important thing to remember is that cosnt modifies the content in front of it. This is what I saw in C ++ templates.

And cosnt int a = 10;

Int const a = 10;

In C ++ templates books, we recommend that you use the following;

Int const * cosnt c = & a; is modified twice and cannot be modified;

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.