As the saying goes, if you do not understand the pointer, you will not learn C/C ++. Therefore, pointer is a very important thing. When C # calls a dynamic link library written by C ++ for quite a year, the pointer in C ++ is the reference type (REF) in C #, but this is not the case after reading it. Pointers are of course inseparable from memory. There are many versions of definitions, but I think the easiest thing to understand is that every variable has a memory address, which is called Pointer .
There are two pointer-related operators: The address operator "&" (used to get the address of a variable) and the pointer operator "*" (using a pointer as its operand, the calculation result indicates the variable to which the operation is directed ). 1 # Include < Iostream. h >
2 Void Main ()
3 {
4 Int A, B, * Pa, * PB;
5 A = 10 ; B = 20 ;
6 Pa = & A; Pb = & B; // Point Pa to A, and point PB to B
7 Cout <* Pa < ' , ' <* PB < ' \ N ' ;
8 Pa = & B; Pb = & A; // Point Pa to B, and point Pb TO
9 Cout <* Pa < ' , ' <* PB < ' \ N ' ;
10 * Pa = 100 ; * PB = 200 ; // Assign values to B and A respectively.
11 Cout < A < ' , ' < B < ' \ N ' ;
12 }
From the above we can see that two variables A and B are declared as int, two pointers Pa, Pb (the pointer type is int *, and the pointer type is int ). Step 1 & A gets the address allocated for A, that is, the address that PA points to a, so * pA = A = 10, similarly * pb = B = 20; in step 2, we can get * pA = B = 20; * pb = A = 10. in step 3, because step 2 points Pa to B and Pb TO A, A = * pb = 200, B = * pA = 100.
Pointers are closely related to arrays. During programming, you can use pointers instead of subscript to reference array elements. Pointers make array usage more flexible and effective. After an array is declared, compile Program Memory space is allocated according to the array type and length. The array name indicates the first address of the array in the memory. When a pointer variable is used to store the first address of the array, this pointer points to this array, and you can indirectly access the elements of the array through pointer operations. The pointer value is the value stored by the pointer itself. This value will be treated as an address by the compiler rather than a general value. In a 32-bit program, the value of all types of pointers is a 32-bit integer, because the 32-bit program's memory address is all 32-bit long. The memory area pointed to by the pointer starts from the memory address represented by the pointer value, and the length is a memory area of sizeof (type pointed to by the pointer. 1 # Include < Iostream. h >
2 Void Main ()
3 {
4 Int A [] = { 2 , 4 , 6 , 8 , 10 };
5 Int * Pa = A; // Or Pa = & A [0]
6 Int Result = 0 ;
7 For ( Int I = 0 ; I < 5 ; I ++ )
8 {
9 Result + = * Pa; // Access array elements through pointers
10 Pa ++ ; // Pointer operation
11 }
12 Cout < " Result = " < Result < ' \ N ' ;
13 }
From the above we can see that we declare an array and pay the value to allocate its memory. A Indicates the first address of the array in the memory, declares a pointer, and points to. Then Pa ++ points to the next address of the array (the int type occupies four bytes ).