A simple summary of the difference between pointer constants and constant pointers in C + + _c language

Source: Internet
Author: User

Let's go back to what is a pointer? What is a constant? A pointer is a special variable in which the contents are stored as memory addresses. A constant is the amount of content stored inside it that cannot be changed. Having understood these two concepts, we are now officially entering pointer constants and constant pointers.

1. The concept of pointer constants and constant pointers

The pointer constant is that the pointer itself is a constant, in other words, the content stored in the pointer (the memory address) is constant and cannot be changed. However, the contents of the memory address can be changed by the pointer.

A constant pointer is a pointer to a constant, in other words, the pointer is a constant, the content it points to cannot be changed, and the content it points to cannot be modified by a pointer. However, the pointer itself is not a constant, and its own value can be changed to point to another constant.

2. Pointer constants and constant pointer declarations

Declaration of pointer constants: data type * Const pointer variable.

Declaration of a constant pointer: data type const * Pointer variable or const data type * pointer variable.

Declaration of constant pointer constants: Data type const * Const pointer variable or const data type * Const pointer variable.

3. Pointer constants and the use of constant pointers

3.1 Examples of pointer constants

/* Pointer Constant example * 
/int a,b; 
int * const P; 
p = &a;//correct 
p = &b;//error 
*p = 20;//correct 

You must assign an initial value when you declare a pointer constant. Using pointer constants can increase the reliability and execution efficiency of your code.

3.2 Example of a constant pointer

/* The example of the constant pointer * 
/int a,b; 
int const *P; 
p = &a;//correct 
p = &b;//correct 
*p = 20;//Error 

A tip about distinguishing pointer constants: the content after the const is not modifiable. For example, the pointer constant int * CONST P = &a; means that the contents of the pointer p cannot be modified; the constant pointer int const *P = &a; Indicates that the content pointed to by the pointer P cannot be modified.

In this, what are pointer constants and constant pointers, and the difference between the two, should be clear and bright.

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.