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 ;}