For beginners. This is the purpose of this Article. Pointers are both difficult and important in C and C ++. SomeProgramThey are proficient in DOS basic.C LanguageSimilar in basic. Only pointers are not available in baisc. Pointer is the soul of C. I don't want to repeat what is clearly stated in most books. I just cannot tell or say anything about the books I have read, and I think I have written something that I understand a little truth. (It is too frustrating to use pointers today.ArticleRecord)
1. pointer Concept
A pointer is a special variable. The value stored in it is interpreted as an address in the memory. To understand a pointer, we need to understand four aspects of the pointer: the pointer type, the pointer type, the pointer value, or the memory zone pointed by the pointer, there is also the memory zone occupied by the pointer itself. Let's explain it separately.First, declare several pointers for example:
Example 1:
(1) int * PTR; (2) char * PTR; (3) int ** PTR; (4) int (* PTR) [3]; (5) int * (* PTR) [4];
|
1. pointer type
From the syntax perspective, you only need to remove the pointer name in the pointer declaration statement, and the rest is the pointer type. This is the type of the pointer. Let's take a look at the type of each pointer in Example 1:
(1) int * PTR; // the pointer type is int * (2) char * PTR; // the pointer type is char * (3) int ** PTR; // The pointer type is int ** (4) int (* PTR) [3]; // the pointer type is int (*) [3] (5) int * (* PTR) [4]; // the pointer type is int * (*) [4]
|
How is it? Is it easy to find the pointer type?
2. Type pointed to by pointer
When you access the memory area pointed to by the pointer, the type pointed to by the pointer determines what the compiler will regard the content in the memory area.
In terms of syntax, you only need to remove the pointer name and the pointer declarative * on the left of the name in the pointer declaration statement, and the rest is the type pointed to by the pointer. For example:
(1) int * PTR; // The Pointer Points to an int (2) char * PTR; // The Pointer Points to a char (3) int ** PTR; // The Pointer Points to the int * (4) int (* PTR) [3]; // The Pointer Points to the INT () [3] (5) type) int * (* PTR) [4]; // The type pointed to by the pointer is int * () [4]
|
In pointer arithmetic operations, the type pointed to by the pointer has a great effect. The pointer type (the pointer type) and the pointer type are two concepts. When you get familiar with C, you will find that, the concept of "type", which is mixed with pointers, is divided into two concepts: "pointer type" and "pointer type", which are one of the key points of mastering pointers.
3. the pointer value, or the memory zone or address pointed to by the pointer
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. Later, we will say that the value of a pointer is XX, which means that the pointer points to a memory area with xx as the first address. We will say that a pointer points to a memory area, it is equivalent to saying that the pointer value is the first address of the memory area.
The memory zone pointed to by the pointer and the type pointed to by the pointer are two completely different concepts. In example 1, the pointer points to a type that already exists, but since the pointer has not been initialized, the memory zone it points to does not exist, or it is meaningless.
In the future, every time you encounter a pointer, you should ask: What is the type of this pointer? What type is the pointer pointing? Where does this pointer point?
4. Memory occupied by pointers
How much memory does the pointer occupy? You just need to use the sizeof function (pointer type) to test it. On a 32-bit platform, the pointer occupies 4 bytes. The memory occupied by pointers can be used to determine whether a pointer expression is left.
Chapter 2 arithmetic operations of pointers
The pointer can be added or subtracted from an integer. The meaning of this operation of pointer is different from that of addition and subtraction of common values. For example:
Example 2:
1. chara [20]; 2.int * PTR = A; ...... 3.ptr++;
|
In the preceding example, the pointer PTR is of the int type * and it points to the int type. It is initialized to the integer variable. In the next 3rd sentences, the pointer PTR is added with 1, and the compiler processes it like this: it adds the value of the pointer PTR with sizeof (INT), in a 32-bit program, is added with 4.
Because the address is in bytes, the address pointed by PTR is increased by four bytes from the address of the original variable A to the high address. Because the length of the char type is one byte, the PTR originally points to the four bytes starting from Unit 0th of array, this point points to the four bytes starting from Unit 4th in array. We can use a pointer and a loop to traverse an array. For example:
Example 3:
Intarray [20]; int * PTR = array;... // the value assigned to the integer array is omitted here.Code. ... For (I = 0; I <20; I ++) {(* PTR) ++; PTR ++ ;}
|
In this example, the value of each unit in the integer array is added to 1. Since every loop adds the pointer PTR to 1, each loop can access the next unit of the array.
Let's look at the example:
Example 4:
1. chara [20]; 2.int * PTR = A; ...... 3.ptr+ = 5;
|
In this example, PTR is added with 5, and the compiler processes it like this: add the value of the pointer PTR to the value of the 5-bysizeof (INT ), in the 32-bit program, 5 is multiplied by 4 = 20. Because the address unit is byte, the current PTR points to the address, compared to the address pointed by the PTR after the addition of 5, move 20 bytes to the high address.
In this example, if the PTR before 5 is not added, it points to the four bytes starting with unit 0th of array A. After 5 is added, the PTR points out of the valid range of array. Although this situation may cause problems in applications, it is possible in terms of syntax. This also reflects the flexibility of pointers.
If in the above example, PTR is subtracted from 5, the processing process is similar, except that the PTR value is subtracted from 5 by sizeof (INT ), the new PTR points to an address that moves 20 bytes to the lower address direction than the original PTR.
To sum up, after a pointer ptrold is added with an integer N, the result is a new pointer ptrnew. The type of ptrnew is the same as that of ptrold, ptrnew points to the same type as ptrold. The value of ptrnew will be increased by N multiplied by sizeof (type pointed to by ptrold) bytes than the value of ptrold. That is to say, ptrnew will point to a memory area that moves n byte by sizeof (type pointed by ptrold) to the high address direction than the memory area pointed to by ptrold.
Chapter 3. Operators & and *
Here & is the address operator, * is... the book is called "indirect operator ". & The operation result of A is a pointer, And the pointer type is a plus *. The Pointer Points to the type, and the Pointer Points to the address, that is the address of.
* P's computation results are varied. In short, the result of * P is what P points to. It has these characteristics: its type is P-pointed, and its occupied address is the address pointed to by P.
Example 5:
INTA = 12; intb; int * P; int ** PTR; P = & A; // The result of & A is a pointer, whose type is int *, the type is int and the address is. * P = 24; // * P result. Here its type is int, and the address occupied by it is the address pointed to by P. Obviously, * P is variable. PTR = & P; // The result of & P is a pointer. the pointer type is p type plus *. Here it is int **. The Pointer Points to the p type. Here it is int *. The Pointer Points to the address of P itself. * PTR = & B; // * PTR is a pointer, and the result of & B is also a pointer, and the types of the two pointers are the same as those pointed, therefore, it is no problem to assign a value to * PTR using & B. ** PTR = 34; // * the result of PTR is what PTR points to. Here it is a pointer, and this pointer is operated again, the result is an int type variable.
|
Chapter 4 pointer expression
If the final result of an expression is a pointer, this expression is called a pointer expression. Below are some examples of pointer expressions:
Example 6:
INTA, B; intarray [10]; int * pA; Pa = & A; // & A is a pointer expression. Int ** PTR = & PA; // & PA is also a pointer expression. * PTR = & B; // * PTR and & B are pointer expressions. Pa = array; PA ++; // This is also a pointer expression.
|
Example 7:
Char * arr [20]; char ** Parr = arr; // If arr is regarded as a pointer, arr is also a pointer expression char * STR; STR = * Parr; // * Parr is the pointer expression STR = * (Parr + 1); // * (Parr + 1) is the pointer expression STR = * (Parr + 2 ); // * (Parr + 2) is a pointer expression.
|
Since the result of a pointer expression is a pointer, the pointer expression also has four elements of the pointer: the pointer type, the type pointed to by the pointer, and the memory area pointed to by the pointer, memory occupied by the pointer itself.
Well, when the result pointer of a pointer expression has clearly occupied the memory of the pointer itself, the pointer expression is a left value, otherwise it is not a left value.
In Example 7, & A is not a left value because it does not occupy clear memory. * PTR is a left value, because * PTR pointer occupies the memory. In fact, * PTR is the pointer Pa. Since PA already has its own location in the memory, * PTR certainly has its own position.
Chapter 5. Relationship between arrays and pointers
The array name can be regarded as a pointer. See the following example:
Example 8:
Intarray [10] = {0, 1, 2, 4, 5, 6, 7, 8, 9}, value ;...... value = array [0]; // It can also be written as: value = * array; value = array [3]; // It can also be written as: value = * (array + 3 ); value = array [4]; // It can also be written as: value = * (array + 4 );
|
In the above example, the array name array represents the array itself, and the type is int [10]. However, if we regard array as a pointer, it points to the 0th units of the array, and the type is int *, the Type pointed to is the type of the array unit, that is, Int. Therefore, it is not surprising that * array is equal to 0. Similarly, array + 3 is a pointer to the array's 3rd units, so * (array + 3) is equal to 3. Others.
Example 9:
Char * STR [3] = {"Hello, thisasample! "," Hi, Goodmorning. "," helloworld "}; chars [80]; strcpy (S, STR [0]); // It can also be written as strcpy (S, * Str); strcpy (S, STR [1]); // It can also be written as strcpy (S, * (STR + 1); strcpy (S, STR [2]); // It can also be written as strcpy (S, * (STR + 2 ));
|
In the preceding example, STR is a three-unit array, and each unit of the array is a pointer, each pointing to a string. If the pointer array name STR is used as a pointer, it points to the cell 0th of the array. Its type is Char **, and it points to char *.
* STR is also a pointer. Its type is char *. It points to Char and its address is the string "Hello, thisasample! ", That is, the address of 'H. STR + 1 is also a pointer pointing to unit 1st of the array. Its type is Char **, and its type is char *.
The following is a summary of the array name problem. If an array typearray [N] is declared, the array name array has two meanings: First, it represents the entire array, and its type is type [N]; second, it is a pointer. the pointer type is type *. The Pointer Points to the type, that is, the type of the array unit. The memory area pointed to by the pointer is the unit 0th of the array, this pointer occupies a separate memory zone. Note that it is different from the memory zone occupied by the array unit 0th.
The value of this pointer cannot be modified, that is, the expression similar to array ++ is incorrect. In different expressions, array names can play different roles.
In the expression sizeof (array), the array name array represents the array itself. Therefore, the sizeof function measures the size of the entire array. In the expression * array, array acts as a pointer. Therefore, the result of this expression is the value of unit 0th of the array. Sizeof (* array) measures the size of the array unit.
Expression Array + N (where n = 0, 1, 2 ,.....) In, array is a pointer, so the result of array + N is a pointer, its type is type *, it points to type, it points to the array number n. Therefore, sizeof (array + n) measures the pointer size.
Example 10:
Intarray [10]; int (* PTR) [10]; PTR = & array;
|
In the above example, PTR is a pointer and its type is int (*) [10]. It points to int [10]. We use the first address of the entire array to initialize it. In the statement PTR = & array, array represents the array itself.
As mentioned in this section, the sizeof () function is used to determine whether the sizeof (pointer name) is the size of the pointer's own type or the size of the type pointed to by the pointer? The answer is the former. For example:
INT (* PTR) [10]; In a 32-bit program, there are: sizeof (INT (*) [10]) = 4 sizeof (INT [10]) = 40 sizeof (PTR) = 4
|
In fact, sizeof (object) measures the size of the object's own type, rather than the size of other types.
* (STR + 1) is also a pointer. Its type is char * and it points to Char, which points to "Hi, Goodmorning. the first character 'h', and so on.
After a pointer ptrold is subtracted from an integer N, the result is a new pointer ptrnew. The type of ptrnew is the same as that of ptrold, and the type of ptrnew is the same as that of ptrold. The value of ptrnew will be less than the value of ptrold by N multiplied by sizeof (type pointed to by ptrold) bytes, that is, the memory area to which ptrnew points will move n times of sizeof (type pointed by ptrold) bytes to the lower address direction than the memory area to which ptrold points.