Const *ptr PTR

Source: Internet
Author: User

1. const int *ptr = NULL; <=> int const *PTR = NULL;

1) Represents a pointer variable to a symbolic constant, and the pointer variable itself is not const so it can point to other variables.

2) The role of Const can be seen as "forbidden by *ptr" To change the value of the variable that the PTR points to, but the variable being pointed to can change its own definition .

eg:const int i = 1, int j = 2;

PTR = &i; Ok

*ptr = +//Error

i = 2//Error i is const

PTR = &j; Ok

*ptr = 28; Error

j = 3; Ok

2. const int *PTR vs int * const PTR;

1) to distinguish between the above two forms of emphasis on the relative position of the const and *:

Const indicates "pointer to symbol constant" before *

Const represents "a constant pointer to a variable of type int" after *

Note: if declared as int * Const PTR = NULL, the value of PTR cannot be changed to point to another variable.

However, you can modify the variable that it points to (if it points to it) by *ptr.

2) Memorizing skills

See if the const modifier is *ptr or ptr?

When const can be assumed to be a const modifier before * *ptr, the value of *ptr is immutable and the value of PTR is variable

When const is assumed to be a const-modified PTR after *, the value of PTR is immutable and the value of *PTR is variable

Const modifies who, whose value cannot be changed.

3. const int * const PTR;

1) indicates "constant pointer to int type symbol constant"

2) const modifies the *ptr to indicate that the value of the pointer to the variable cannot be modified by *ptr

It also modifies the PTR to indicate that the value in the PTR can no longer be modified to point to another variable

Const *ptr PTR

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.