What does const int * p mean?

Source: Internet
Author: User

Let's take a look at a line of code:

Const int * p = new int (10 );

The problem arises. The new address should be block heap memory, and the const constant should be stored in the static memory zone, is the memory pointed to by the pointer p in the static zone or heap?

Let's take a look at the usage of const?

  1. Const constants can be defined.
  2. Const can modify the parameters, return values, and even the definition body of a function. All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.

In fact, it is very easy to think about it. const int * p is a constant pointer, that is, through which the target cannot be modified, but without saying that the target pointed by the pointer must be a constant.

Int n = 100; // n is not a constant const int * p1 = & n; // p1 points to nint * p2 = & n; // p2 points to nn = 200; // OK, n is not a constant, you can modify * p1 = 300; // error, p1 is a constant pointer, and its target cannot be modified * p2 = 400; // OK, p2 is not a constant pointer and its target can be modified.

Therefore, const int * p defines a constant, which indicates that the content pointed to by p cannot be modified. The int memory to be pointed to should be in the heap. The pointer type determines the behavior of the pointer. As for the target to which the Pointer Points, what should be done? It has nothing to do with this pointer.

For more information, see C pointer: const int * pi semantics.

Related Article

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.