Refer to C ++ primer 4th (Chinese Version)
1. c ++ pointer meaning: the pointer stores the address of another object. Reference Type
* Get the referenced object and get the address
2. Each pointer has a data type associated with it, and the pointer can only point to this type of object.
Note: string * ps1, pS2;
In fact, PS1 is defined as a pointer. PS2 is not a pointer, but a common string object. String * ps1, * PS2;
In this way, two pointers are defined.
3. If the pointer value is null, It is equal to or equal to 0. The uninitialized pointer is invalid (storing an uncertain address). Avoid using invalid pointers in the program. So try to initialize the pointer before use.
4. Void * pointer: the address of any type of object can be saved.
Operation restrictions: Compare with another pointer;
Pass the void * pointer to the function or return the void * pointer from the function;
Assign a value to another void * pointer.
You are not allowed to use the void * pointer to operate the objects it refers.
5. assign values to pointers and assign values through pointers.
A) if the operands are unreferenced, the value of the object referred to by the pointer is modified;
Example: String S1 ("hello"); string * SP1 = & S1;
String S2 ("world"); * SP1 = S2;
In this case, the address stored in the pointer SP1 is still the address of S1, but the value of S1 is modified. In this case, S1 = "wolrd ";
B) if the unreferenced operation is not used, the value of the pointer is modified.
Example: String S1 ("hello"); string * SP1 = & S1;
String S2 ("world"); SP1 = & S2;
In this case, the address stored in the pointer SP1 is changed from the S1 address to the S2 address, and the S1 value remains unchanged.
6. differentiate pointers and references
References always point to an object and must be initialized during definition. In addition, the value assigned to a reference is to modify the value of the object associated with the reference, not to associate the reference with another object.
Example: Int & rI = ival; Int & ri2 = ival2;
Ri = ri2; // modify the value of ival to be the same as that of ival2. Ri is still the referenced ival
7. Double pointer: pointer to the pointer.
Int ival = 1024; int * P = & ival; int ** pp = & P;
In this case, the memory address of ival is stored in P, and the address of pointer P is stored in pp. Obtain the value of ival after two decomquotes.
Example: ** pp = ival;
This is true.
And so on: There are three pointers and so on. Of course they are not commonly used. Be careful when using them.
8. pointers and Arrays
A) C ++ automatically converts an array name to a pointer to the first element of the array.
B) If int arr [5] = {0, 1, 2, 3, 4}; int * P = arr;
The pointer P is added or subtracted to change the element. Add 1 to point to the next element, and subtract 1 to point to the previous element (Avoid crossing the border ). ++ ,--
The operation is the same.
C) two pointers pointing to the same array can be used for subtraction. For example: int * P2 = & arr [4];
Then, P2-p1 = 4; is true. The result of the subtraction operation is of the ptrdiff_t type (signed integer type, which can be a negative number ).
D) allow pointer addition and subtraction 0. The result of two null pointers is still 0.
E) when the subscript of the array is used to access the element, it is actually used to perform the subscript operation on the pointer to the array element.
F) C ++ allows calculation of the address of an array or object that exceeds the end, but does not allow unreferencing of this address. The address after the calculated array exceeds the end position or before the first address of the array is invalid. The next element at the end of an array is often used as a "Sentinel ".
9. pointer and const Modifier
A) const int * P; // P is not Const. You can change the object pointed to by P without initializing p;
B) You cannot use the void * pointer to save the address of the const object. You must use the const void * pointer to save the address of the const object.
C) allow the non-const object address to be assigned to the pointer to the const object, but cannot change the value to the object through P:
Int A = 3; P = &;
A = 4; // * P = 5; // Error
D) const pointer: Once the pointer is initialized, the object to which the pointer cannot be changed.
Int c = 8; int * const CP = & C;
CP can only point to C and cannot point to other objects. You can change the value of C through CP.
E) const pointer to the const object: neither the value of the object indicated by the pointer nor the pointer to another object can be modified:
Const int d = 6; constint * const dp = & D;
Int c = 9; dp = & C; // Error * dp = 7; // Error
F) Note: typedefstring * pstring;
Const pstring CSTR;
The True Type of CSTR is: string * const CSTR;
Instead of const string * CSTR;