Differences between pointer constants and constant pointers

Source: Internet
Author: User

Differences between pointer constants and constant pointers

Int a, B;

Int * const p1 = & a; // p1 is a pointer constant.

Int const * p2 = & B; // p2 is a constant pointer.

 

Recognize constant pointers and pointer constants. Remember the following three sentences:

Pointer (*) and constant (const) who read before;

Pointer (*) before (int * const p1 = & a) indicates that the pointer cannot be changed and cannot be directed to another address, but the value of the indicated address can be changed through the pointer;

A constant (const) before (int const * p2 = & B) indicates that the value of the indicated address cannot be changed through the pointer, but the pointer can point to other places.

Example:

 

# Include <iostream> using namespace std; int main () {int a = 1; int B = 2; int const * p1 = & B; // constant pointer p1 cout <"* p1 =" <* p1 <endl; // p1 points to B, and the output value p1 = &; // you can change the p1 pointer of a constant to point to another location. cout <"* p1 =" <* p1 <endl; // p1 now points to, the output value of a is int * const p2 = & B; // pointer constant p2 cout <"* p2 =" <* p2 <endl; // p2 points to B, output the value of B * p2 = 3; // you can use the pointer constant p2 to change the value of the indicated object cout <"* p2 =" <* p2 <endl; // p2 also points to B, but the value of B changes to 3 return 0 ;}

 

2.Returns a constant's function.Can be a constant pointer, pointer constant, constant, form:
Const type * funcname (type1 arg1, type2 arg2 ,..)
Type * const funcname (type1 arg1, type2 arg2 ,..)
Const funcname (type1 arg1, type2 arg2 ,..)

 

3. Constant pointers and pointer constants are assigned values so far.

Both constant pointers and pointer constants can be assigned to constant pointer objects. Constant pointer objects can be p ++, but cannot be changed through * p.
Constant pointers and pointer constants can be assigned to pointer constants, but pointer constants can only perform * p operations, not p ++ operations.
A normal type of function that returns constants. The purpose is to prevent calculation between the return values of a member function from generating ugly code.

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.