ArticleDirectory
- Instance 1:
- Instance 2:
- Instance 3:
- Instance 4
For pointers and constants, the following three forms are correct:
Const Char* Myptr = &Char_a; // pointer to a constantChar*ConstMyptr = &Char_a; // constant pointerConst Char*ConstMyptr = & 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,For
1. Constant pointer (Constant pointers)
Int*ConstP
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: View code
# Include <iostream> # Include <Stdlib. h> Using Namespace STD; Void Main (){ Int I1 = 30 ; Int I2 = 40 ; Int * Const Pi = & I1; // Here, the PI pointer constant. // Pi = & I2; // Note that here, pi can no longer be re-assigned, that is, it can no longer point to another new address. So I have commented on it. Printf ( " % D \ n " , * PI ); // 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: View code
CharChar_a ='A';CharChar_ B ='B';Char*ConstMyptr = &Char_a; myptr= & Char_ B;//Error-can't change address of myptr
2. pointer to a constant (pointers to constants)
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: View code
# Include <iostream> # Include <Stdlib. h> Using Namespace STD; Void Main (){ Int I1 = 30 ; Int I2 = 40 ; Const Int * Pi = & I1; printf ( " % D \ n " , * PI ); // 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 4
CharChar_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:
CharA [] ="I love you!";//Defines a character arrayChar* P ="I love you!";//Defines a character pointer.
A can be interpreted as a constant pointer, while P is a pointer to a constant,CodeExample:
View code
# 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, so comment out it 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 its value cannot be modified. Cout <A <Endl; cout <P < Endl; cout <[ 1 ] < Endl; cout <* (P + 2 ) < Endl; System ( " Pause " );}
The output value is:
Ialove you!
I love you!
A
L