The learning method of computer pointers. Concept of pointer
The pointer is a special variable, which is interpreted as an address in memory. To figure out a pointer, you need to figure out the four aspects of the pointer: the type of pointer, the type pointed to by the pointer, the value of the pointer or the memory area pointed to by the pointer, and the memory area occupied by the pointer itself. Let us explain separately. First declare a few pointers to the example: Example 1: (1) INT * PTR; (2) char * PTR; (3) int ** PTR; (4) INT (* PTR); (5) INT * * PTR); If you don't understand a few examples, please refer to the article I posted for some time. "How to understand the complex type declaration of C and C >>.
1. Type of pointer. From a grammatical point of view, you only need to remove the pointer name in the statement statement, and the remaining part is the type of this pointer. This is the type of pointer itself. Let's take a look at the type of each pointer in Example: (1) INT * PTR; / / The type of pointer is int * (2) char * PTR; / / pointer type is char * (3) int ** PTR; / / The type of pointer is int ** (4) int (* ptr); / / The type of pointer is int (*) (5) int * (* PTR); / / The type of pointer is int * (*) kind? Is it very simple to find the type of a pointer?
2. The type pointed to the pointer. When you use a pointer to access the memory area pointed to by the pointer, the type pointed to by the pointer determines what the compiler will treat the content in the memory area. From the grammar, you only need to remove the pointer name and the pointer declare * on the left side of the name of the pointer statement. The remaining is the type pointed to by the pointer. For example: (1) INT * PTR; / / The type pointed to by the pointer is int (2) char * PTR; / / The type pointed to by the pointer is the CHAR (3) int ** PTR; / / pointer points to The type is int * (4) int (* ptr); / / The type pointed to by the pointer is int () (5) int * (* PTR); / / The type pointed to by the pointer is int * () in the pointer In the arithmetic operation, the type pointed to by the pointer has a large role. The type of pointer (ie, the type of pointer itself) and the type pointed to by the pointer are two concepts. When you are more familiar with C, you will find that the "type" "type" of the pointer stir together is divided into "the type of pointer" and "the type pointed to the pointer", which is the key point of the proficiency pointer. one. I have read a lot of books, I found some books that were poor, and the two concepts of the pointer were stirred together, so I looked at the conflict before and after, the more I saw it.
3. The value of the pointer or the memory area or address pointed to by the pointer. The value of the pointer is the value stored in the pointer itself, which will be used as an address by the compiler, not a general value. In a 32-bit program, all types of pointers are a 32-bit integer because the memory address is all 32-bit in the 32-bit program. The memory area pointed to by the pointer begins with the memory address represented by the value of the pointer, a memory area of the length of Si Zeof (the type pointed to by the pointer). In the future, we said that the value of a pointer is XX, which is equivalent to the memory area that is the address of XX as XX; we say that a pointer points to a block memory area, which is equivalent to this pointer is this The first address of the memory area. The type of memory pointed to the pointer and pointer is two completely different concepts. In the example one, the type pointed to by the pointer is already, but since the pointer has not been initialized, the memory area indicted to it does not exist, or meaningless. In the future, every time a pointer should ask: What is the type of this pointer? What is the type pointing to the pointer? Where is this pointer pointing? 4. The memory area occupied by the pointer itself. How much memory itself does the pointer itself? You only need to use a function Sizeof (type of pointer) to know. In the 32-bit platform, the pointer itself occupies 4 bytes of length. This concept of memory itself is useful in determining whether a pointer expression is a left value. Arithmetic operation of pointer
The pointer can add or minus an integer. The meaning of this operation of the pointer and the meaning of the usual value of the usual value are different. For example: Example 2: 1. CHAR A; 2. INT * PTR = a; ... 3. PTR ; In the above example, the type of pointer PTR is int *, which point points to INT, which is initialized to point to the intersive variable A. In the next article 3, the pointer PTR is added 1, the compiler is processed: it adds the value of the pointer PTR with SIZEOF (int), in the 32-bit program, is added 4. Since the address is in units of unit, the address pointed to by the PTR increases by 4 bytes from the address of the original variable A. Since the length of the CHAR type is one byte, the original PTR is four bytes that point to No. 0 unit of array A. At this point, at this point, at this point, at this point, at this point, the four bytes starting from the number 4 unit. We can use a pointer and a loop to traverse an array, see example: Example 3: Example 3: int Array; int * ptr = array; ... // This is a code that is omitted to assign values. ... for (i = 0; i <20; i ) {(* PTR) ; PTR ;} This example adds the value of each unit in the integer array 1. Since each loop is plugged in 1, the next unit of the array can be accessed at each loop. Look at the example: Example 4: 1. CHAR A; 2. INT * PTR = a; ... 3. PTR = 5; In this example, PTR is added 5, the compiler is processed: adding the value of the pointer PTR plus 5 by sizeof (int), in the 32-bit program plus 5 multiplication 4 = 20. Since the unit of the address is byte, the address pointed to by the current PTR moves 20 bytes to the high address direction than the address pointed to the PTR after adding 5. In this example, it is not added to the four bytes starting from the PTR before 5, and the PTR has pointed to the legal range of the array A. Although this situation will ask questions on the application, it is ok. This also reflects the flexibility of the pointer. If in the previous example, PTR is subtracted 5, then the processing process is similar, but the value of the PTR is subtracted to be subjected to 5 multiply SizeOf (int), the new PTR pointing is lower than the address pointed to by the original PTR. The address direction moves 20 bytes. Summary, after a pointer Ptrold couples an integer N, the result is a new pointer PTRNEW, PTRNEW type, and the type of Ptrold, the type pointed to by PtrNew and the type pointed to by Ptrold. The value of the PTRNEW will add N-multiply SIZEOF (Type of Ptrold) by the value of Ptrold. That is to say, the memory area pointed to by PtrNew will move n-multiply SIZEOF (Type of Ptrold) by the memory area pointed to by PTROLD. After a pointer Ptrold subtracts an integer N, the result is a new pointer PTRNEW, PTRNEW type and Ptrold type, and the type pointed to by PtrNew and the type pointed to by Ptrold. The value of PT RNEW will reduce N-multiply SIZEOF (Type of Ptrold) by the value of Ptrold, that is, the memory area pointed to by PtrNew will move n multiply from the memory area pointed to by the PTROLD. SIZ EOF (Type Point to Ptrold) One byte. third chapter. Operator & and *
Here & is to take the address operator, * is ... The book is called "indirect operator". The calculation result of & a is a pointer, the type of pointer is the type of A plus a *, the type pointed to by the pointer is the type of A, the address pointed to by the pointer, that is, the address of A. * P The calculation result is a five-flowers. In short, the result is something pointed to P, this feature has these features: its type is the type P pointing, the address it occupies is the address pointed to by P. Example 5: INT A = 12; int b; int * p; int ** PTR; P = & a; // & a result is a pointer, the type is int *, the point to the type is int, the pointing address is A address. * P = 24; /// * p The result, here it is int, the address it occupies is the address pointed to by P, apparent, * P is the variable A. PTR = & P; // & p The result is a pointer, the type of the pointer is the type P, which is added *, here is int **. The type pointed to this pointer is the type of P, which is int *. The address points to the pointer is the address of the pointer P. * ptr = & b; // * Ptr is a pointer, and the result of & B is also a pointer, and the type of these two pointers is the same, so use & b to give * PTR assignment is no problem. ** PTR = 34; // * PTR's result is something pointed to by PTR, here is a pointer, and then doing this pointer again * calculation, the result is an int type variable. Chapter Four. Pointer expression. The last result of an expression If it is a pointer, then this expression is called a pointer expression. Here are some examples of pointer expressions: Example 6: Int A, B; int Array; int * pa; pa = & a; // & a is a pointer expression. INT ** PTR = & Pa; // & pa is also a pointer expression. * PTR = & b; // * PTR and & B are both a pointer expression. PA = Array; PA ; // This is also a pointer expression. Example 7: char * arr; char ** parr = arr; // If arr is regarded as a pointer, Arr is a pointer expression char * STR; str = * parr; // * Parr is a pointer expression Str = * ( PARR 1); // * (PARR 1) is a pointer expression Str = * (PARR 2); // * (PARR 2) is a pointer expression
Since the result of the pointer expression is a pointer, the pointer expression also has four elements of the pointer: the type of pointer, the type pointed to by the pointer, the memory area indicated by the pointer, the memory itself occupies the memory itself. Ok, when a pointer expression results pointer have explicitly have the memory itself, this pointer expression is a left value, otherwise it is not a left value. In Example 7, & A is not a left value because it has not yet occupied a clear memory. * Ptr is a left value, because * PTR This pointer has occupied memory, in fact, * PTR is a pointer PA, since PA has already had its own position in the memory, then * PTR is of course its own position. .
The last result of an expression If it is a pointer, then this expression is called a pointer expression. Here are some examples of pointer expressions: Example 6: Int A, B; int Array; int * pa; pa = & a; // & a is a pointer expression. INT ** PTR = & Pa; // & pa is also a pointer expression. * PTR = & b; // * PTR and & B are both a pointer expression. PA = Array; PA ; // This is also a pointer expression. Example 7: char * arr; char ** parr = arr; // If arr is regarded as a pointer, Arr is a pointer expression char * STR; str = * parr; // * Parr is a pointer expression Str = * ( PARR 1); // * (PARR 1) is a pointer expression str = * (PARR 2); // * (PARR 2) is a pointer expression because the result of the pointer expression is a pointer, so pointer The expression also has four elements of the pointer: the type of pointer, the type pointed to by the pointer, the memory area indicated by the pointer, the memory itself occupies. Ok, when a pointer expression results pointer have explicitly have the memory itself, this pointer expression is a left value, otherwise it is not a left value. In Example 7, & A is not a left value because it has not yet occupied a clear memory. * Ptr is a left value, because * PTR This pointer has occupied memory, in fact, * PTR is a pointer PA, since PA has already had its own position in the memory, then * PTR is of course its own position.