[C ++] What about the C ++ pointer? Three values related to constants, variables, pointers, and pointers

Source: Internet
Author: User

Constant: Only data occupies storage space, but after compilationCodeOnly reference data, no address concept. For example:

Const string HI = "hello ";

Const int days = 31;

 

Variable: the data stored in the memory (right value). The address (left value) needs to be referenced during modification. For example:

Int num = 12;

Float d = 354.333f;

Bool B = true;

 

The pointer should focus on three values: the memory address for storing the pointer variable, the memory address pointed to by the pointer, and the value pointed to by the pointer.

If a pointer variable is defined:

Int num = 8;

Int p * = & num;

The three values are expressed as: & P, P, * P.

Example:

Int main () {int * P = NULL; cout <"not assigned after initialization:" <Endl; cout <"pointer variable address (& P) = "<& P <Endl; cout <" pointer to address (p) = "<p <Endl; If (P! = NULL) cout <"pointer to address storage value (* P) =" <* P <Endl; elsecout <"null pointer, cannot be set. "<Endl; P = new int (1024); cout <" after assignment: "<Endl; cout <" pointer variable address (& P) = "<& P <Endl; cout <" pointer to address (p) = "<p <Endl; If (P! = NULL) cout <"pointer to address storage value (* P) =" <* P <Endl; elsecout <"null pointer, cannot be set. "<Endl; Delete P; P = NULL; cout <" after release: "<Endl; cout <" pointer variable address (& P) = "<& P <Endl; cout <" pointer to address (p) = "<p <Endl; If (P! = NULL) cout <"pointer to address storage value (* P) =" <* P <Endl; elsecout <"null pointer, cannot be set. "<Endl; return 0 ;}

You can clearly understand the relationships and differences between the three.

Note: it is best to assign null values when defining a pointer, and assign null values when releasing the pointer (delete). In this way, some incorrect pointer actions can be avoided.

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.