Pointers and const in C + +

Source: Internet
Author: User

When I first approached C + +, the relationship between the pointer and the const was a bit confusing, as summarized below:

A pointer to a const variable

#include <iostream.h>voidMain () {Const int*p=NULL; Const intA=Ten; P=&A; cout<<"*p="<<*p<<Endl; intb= -; P=&b; cout<<"*p="<<*p<<Endl; //*p=200; Error, you cannot modify the pointer to change the contents of the pointerb= $; cout<<"*p="<<*p<<Endl;}

You cannot modify the pointer to the content that the pointer points to, but you can modify the pointer's point.

Another form:

int const *p=null;

Second, the const pointer

#include <iostream.h>voidMain () {intA=Ten; intb= -; int*Constp=&A; cout<<"*p="<<*p<<Endl; //p=&b; Error, you cannot modify the point of a const pointer to change what the pointer points to *p= $; cout<<"*p="<<*p<<Endl;}

You cannot modify the pointer's point, but you can modify the pointer to the content that the pointer points to.

However, this will also be an error:

#include <iostream.h>voidMain () {Const intA=Ten; intb= -; int*Constp=&A; cout<<"*p="<<*p<<Endl; //p=&b; Error, you cannot modify the point of a const pointer to change what the pointer points to *p= $; cout<<"*p="<<*p<<Endl;}

The error is as follows:

--------------------Configuration:01-win32 Debug--------------------
Compiling ...
01.cpp
E:\Program Files (x86) \20141102\01.cpp (6): Error C2440: ' Initializing ': cannot convert from ' const int * ' to ' int *const ‘
Conversion loses qualifiers
Error executing cl.exe.

01.exe-1 error (s), 0 warning (s)

There is no such problem with pointers to const variables.

Third, const pointer to const variable

 #include <iostream.h>void   main () {  int  a=10     ;  int  b=100     ;  const  int  *    const  p=&A; cout  << " *p=  "  <<*p<<ENDL;     // p=&b;    Error, you cannot modify the point of the const pointer to alter the contents of the pointer to  // *p=200; Error, you cannot modify the pointer to the content that the pointer is pointing to } 

You cannot modify the pointer to change what the pointer points to, nor can you modify the pointer's point.

I guess I didn't have the patience to look at it, maybe it wasn't a thing at the time.

Copyright Notice:

Visitors may use the content or services provided by this homepage (HTTP://WWW.CNBLOGS.COM/WANGSHUNLI) for personal learning, research or appreciation, and for other non-commercial or non-profit purposes,

At the same time, it shall abide by the provisions of the copyright law and other relevant laws and shall not infringe the legal rights of this homepage and the relevant rights holders.

Be sure to notify this home page and make a hyperlink in the form of the content from this home page , so as not to cause unnecessary trouble.

E-mail:[email protected]

Pointers and const in C + +

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.