For pointers and constants, the following three forms are correct:
Copy codeThe Code is as follows: const char * myPtr = & char_A; // pointer to a constant
Char * const myPtr = & char_A; // constant pointer
Const char * const myPtr = & char_A; // constant pointer to a constant
The three types are described in sequence below.
Because * The operator is the left operator, the priority of the Left operator is from right to left.
1. Constant pointer (Constant Pointers)
Copy codeThe Code is as follows: int * const p
Look at const first and then *. p is a constant type pointer. You cannot modify the pointer, but the stored value on the address pointed by this pointer can be modified.
Instance 1:Copy codeThe Code is as follows: # include <iostream>
# Include <stdlib. h>
Using namespace std;
Void main ()
{
Int i1 = 30;
Int i2 = 40;
Int * const pi = & i1; // The pi pointer constant here.
// Pi = & i2; // note that here, pi cannot be assigned again, that is, it cannot point to another address. So I have commented on it.
Printf ("% d \ n", * pi); // The output is 30
I1 = 80; // 5. Think about it: Can I use * pi = 80; instead? Yes. Here, you can modify the i1 value through * pi.
Printf ("% d \ n", * pi); // The output is 80
System ("pause ");
}
Instance 2:Copy codeThe Code is as follows: char char_A = 'a ';
Char char_ B = 'B ';
Char * const myPtr = & char_A;
MyPtr = & char_ B; // error-can't change address of myPtr
2. pointer to a constant (Pointers to Constants)
Copy codeThe Code is as follows: const int * p
First look at * and then look at const, and define a pointer pointing to a constant. You cannot use a pointer to modify the value pointed to by this pointer.
Instance 3:Copy codeThe Code is as follows: # include <iostream>
# Include <stdlib. h>
Using namespace std;
Void main ()
{
Int i1 = 30;
Int i2 = 40;
Const int * pi = & i1;
Printf ("% d \ n", * pi); // The output is 30
Pi = & i2; // note that pi can assign a new memory address at any time.
I2 = 80; // think about it: Can I use * pi = 80; instead? Of course not.
Printf ("% d \ n", * pi); // The output is 80
System ("pause ");
}
Instance 4Copy codeThe Code is as follows: char char_A = 'a ';
Const char * myPtr = & char_A;
* MyPtr = 'J'; // error-can't change value of * myPtr
Therefore, the integer number pointed to by pointer p is a constant and its value cannot be modified.
3. Constant pointer to a constant
For "constant pointer to a constant", the content in both 1 and 2 must be satisfied, neither the pointer value nor the value pointed to by the pointer can be modified.
4. Introduce character arrays and character pointers
Character arrays and character pointers are defined as follows:Copy codeThe Code is as follows: char a [] = "I Love You! "; // Defines an array of Characters
Char * p = "I Love You! "; // Defines a character pointer
A can be interpreted as a constant pointer, while p is a pointer to a constant. The code example is as follows:Copy codeThe Code is as follows: # include <iostream>
# Include <stdlib. h>
Using namespace std;
Void main ()
{
Char a [] = "I Love You! "; // Defines an array of characters. array name a is a constant pointer and points to the same position. It is the position of the first element of the array.
Char * p = "I Love You! "; // Defines a character pointer. the pointer p points to a String constant, which cannot be modified.
// * (P + 1) = 'a'; // error. You cannot modify the value pointed to by the pointer. Therefore, comment it out here.
A [1] = 'a'; // constant pointer. You cannot modify the pointer value, but you can modify the value pointed to by the pointer.
// A = p; // error. a is a constant pointer and cannot be modified.
Cout <a <endl;
Cout <p <endl;
Cout <a [1] <endl;
Cout <* (p + 2) <endl;
System ("pause ");
}
The output value is:
IaLove You!
I Love You!
A
L