Learning notes-const pointer, const int * P, int * const P, int const * P

Source: Internet
Author: User

 

# Include <iostream>
Using namespace STD;
Int main ()
{

Int int1 = 1;
Int int2 = 0;
Const int * pint1 = & int1;
Int * const pint2 = & int2;
Cout <"* pint1 =" <* pint1 <Endl;
Cout <"* pint2 =" <* pint2 <Endl;
* Pint1 = 2;
Pint2 = & int1;
Cout <"* pint1 =" <* pint1 <Endl;
Cout <"* pint2 =" <* pint2 <Endl;

Int N; // to prevent the screen from flashing
Cin> N;
Return 0;
}

The following error will pop up during this operation:

1> ------ generated: Project: vs2008, configuration: Debug Win32 ------
1> compiling...
1> main. cpp
1> G: \ learning \ vs2008project \ vs2008 \ vs2008 \ main. cpp (12): Error c3892: "pint1": cannot assign a value to a constant
1> G: \ learning \ vs2008project \ vs2008 \ vs2008 \ main. cpp (13): Error c3892: "pint2": cannot assign a value to a constant
1> the generated logs are stored in "file: // G: \ \ vs2008project \ vs2008 \ vs2008 \ debug \ buildlog.htm"
1> vs2008-2 errors, 0 warnings
============ Generate: 0 successful, 1 failed, 0 latest, skipped 0 ==========

Here we will illustrate

Pint1 is a pointer to an integer variable. The value it points to cannot be changed, that is, * pint1 cannot be modified, and pint1 can be changed to the address pointed to. That is, pint1 can be modified.

Pint2 is a constant pointer to an integer. the pointer is a constant, that is, the address pointed to by the pointer cannot be changed, that is, the value of pint2 cannot be changed, but * pint2 can be modified.

This is understandable.

For the definition of pint1, the right side of const is int, so the value that pint1 points to is a constant

For the definition of pint2, the right side of const is a variable, so this variable is a constant. What is this variable? After reading the complete definition, it is a pointer, so this is a pointer constant.

Next let's take a look.

Int const * P

# Include <iostream>
Using namespace STD;
Int main ()
{

Int int1 = 1;
Int int2 = 0;
Const int * pint1 = & int1;
Int * const pint2 = & int2;
Cout <"* pint1 =" <* pint1 <Endl;
Cout <"* pint2 =" <* pint2 <Endl;
* Pint1 = 2; // The number of rows is 12th.
Pint2 = & int1; // 13th rows
Cout <"* pint1 =" <* pint1 <Endl;
Cout <"* pint2 =" <* pint2 <Endl;
Int const * pint3 = & int1;
* Pint3 = 2; // The number of rows is 17th.
Pint3 = & int1;
Cout <"* pint3 =" <* pint3 <Endl;
Int N; // to prevent the screen from flashing
Cin> N;
Return 0;
}
The pop-up error is as follows:

1> ------ generated: Project: vs2008, configuration: Debug Win32 ------
1> compiling...
1> main. cpp
1> G: \ learning \ vs2008project \ vs2008 \ vs2008 \ main. cpp (12): Error c3892: "pint1": cannot assign a value to a constant
1> G: \ learning \ vs2008project \ vs2008 \ vs2008 \ main. cpp (13): Error c3892: "pint2": cannot assign a value to a constant
1> G: \ learning \ vs2008project \ vs2008 \ vs2008 \ main. cpp (17): Error c3892: "pint3": cannot assign a value to a constant
1> the generated logs are stored in "file: // G: \ \ vs2008project \ vs2008 \ vs2008 \ debug \ buildlog.htm"
1> vs2008-3 errors, 0 warnings
============ Generate: 0 successful, 1 failed, 0 latest, skipped 0 ==========

This shows that
Int const * P and int * const P are consistent, so you only need to check what is on the right of const to determine

In int const * P, the right side of const is * P, so P is a pointer constant.

In int * const P, the right side of const is P, so this variable is a constant and this variable is a pointer, so this is also a pointer constant.

In const int * P, the right side of const is int, so this integer is a constant, so the value of this * P cannot be changed;

 

Thank you

Author: Lin yufei
Source: http://www.cnblogs.com/zhengyuhong/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. However, you must keep the author's information without the author's consent and provide the original article connection clearly on the article page.

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.