Constant pointer and pointer constant

Source: Internet
Author: User
Tags constant definition

1. Constant pointer definition: A pointer that can only read data in the memory but cannot modify the attributes of data in the memory. It is called a pointer to a constant, or a constant pointer for short. Declaration: const int * p; Note: You can assign the address of a constant to a constant pointer of the corresponding type, because the constant pointer cannot modify the internal coarse data through the pointer. It can only prevent modifying data in memory through pointer reference, and does not protect the objects pointed to by the pointer. 2. pointer constant definition: A pointer constant means that the position pointed to by the pointer cannot be changed. That is, the pointer itself is a constant, but the content pointed to by the pointer can be changed. Declaration: int const * p = & a; Note: pointer constants must be initialized at the same time as they are declared. a pointer constant cannot be declared before being assigned a value, this is the same as a normal constant. 3. Example

Int _ tmain (int argc, _ TCHAR * argv []) {// defines the variable int a = 1; // defines the constant const int B = 2; // define the constant pointer const int * ptr1 = & a; // define the pointer constant. The value must be int * const ptr2 = & a; // error, you cannot assign the constant address to the pointer variable int * ptr3 = & B; // correct. You can assign the constant address to the constant pointer const int * ptr4 = & B; // error. indirect reference to the constant pointer cannot modify the data in the memory * ptr1 = 3; // correct. indirect reference to the pointer constant can modify the data in the memory * ptr2 = 4; // correct. The constant pointer can point to another variable ptr1 = & B; // error. The pointer constant cannot point to another variable ptr2 = & B; // constant pointer constant, that is, it cannot indirectly reference or modify memory data, nor point to other variables const int * const ptr5 = & a; // error. It cannot indirectly reference or modify memory data * ptr5 = 5; // error. You cannot modify the object ptr5 = & B; return 0 ;}

 

 

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.