Pointer constants and constant pointers

Source: Internet
Author: User

Do not worry about whether the code you write contains a pointer constant or a constant pointer. First understand your code.

Let's take a look at these declaration methods:


[Cpp]
Int * pi1;
Int * pi2;

Int * pi1;
Int * pi2;


The results of the two declaration methods are the same. Both pi1 and pi2 are integer pointers. The so-called integer pointer is the pointer variable pointing to the first address of the integer array. In fact, both int * and int * describe a variable, indicating the attributes of a variable.

 


Continue to look down


[Cpp]
Int const * pid1;

Int const * pid1; [cpp] view plaincopyprint? Const int * pid2;

Const int * pid2;
The two statements have the same result. First, both pid1 and pid2 are pointers, (* pid1) and (* pid2) both have int attributes, so both pointers are integer pointers, because the const attribute is added, the integer (* pid1) cannot be modified.
This is a constant pointer.

 


Let's take a look at the following statement.


[Cpp]
Int * const pid;

Int * const pid;
The pid is a pointer, And the modifier of the pid is const. Therefore, the pid content cannot be modified. That is to say, it can only be stored once and initialized during declaration.

This is a pointer constant.

 


Summary:

A pointer constant is a pointer of a constant.

A constant pointer is a pointer to a constant.

In fact, after understanding the above explanation, you don't have to worry about constant pointers and pointer constants. Yes ..

 

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.