It is estimated that many people have seen this post. Two years ago, after reading the post, the Birdman recorded it as a local doc and now shared it .....
Chapter 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];
If you do not understand the following examples, refer to the articles I posted earlier
++ Complex type declaration>.
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. The type to which the Pointer Points.
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 type.
(2) char * ptr; // The Pointer Points to a char type.
(3) int ** ptr; // The type pointed to by the pointer is int *
(4) int (* ptr) [3]; // The type pointed to by the pointer is int () [3]
(5) 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. I have read a lot of books and found that some poorly written books bring together the two concepts of pointers. Therefore, the books seem to have conflicts and become more confused.
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. The memory occupied by the pointer.
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 on 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. Char a [20];
2. Int * ptr =;
...
...
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:
Int array [20];
Int * ptr = array;
...
// The code for assigning values to integer arrays is omitted here.
...
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. Char a [20];
2. Int * ptr =;
...
...
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. 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.
The types to be pointed to are also the same. 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, ptrnew moves n times sizeof (type pointed by ptrold) bytes to the lower 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:
Int A = 12;
Int B;
Int * P;
Int ** PTR;
P = & A; // The result of & A is a pointer. The 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 to, so? It is no problem to assign a value to * PTR from amp; 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:
Int a, B;
Int array [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 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 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
If you do not understand the statements for declaring arrays, see the article I posted earlier
Add Scary Birds:
Int array [10] [15]; If array is used as a pointer, It is a pointer to int [15], that is:
Int (* ptr) [15] = array;
Example 8:
Int array [10] = {,}, 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, this is a sample! ",
"Hi, good morning .",
"Hello world"
};
Char s [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 string "Hello, this is asample! ", 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 *.
* (Str + 1) is also a pointer. Its type is char * and it points to char, which points to "Hi, good morning. the first character 'h', and so on.
The following is a summary of the array name problem. If an array TYPE array [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:
Int array [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.
Chapter 6. Relationship between pointer and Structure Type
You can declare a pointer to a structure object.
Example 11:
Struct MyStruct
{
Int;
Int B;
Int c;
}
MyStruct ss = {20, 30, 40}; // declares the structure object ss, and initializes the three Members of ss to 20, 30, and 40.
MyStruct * ptr = & ss; // declares a pointer to the structure object ss. Its type is
MyStruct *, which points to the type of MyStruct.
Int * pstr = (int *) & ss; // declares a pointer to the structure object ss. However, its type is different from the type it points to and ptr.
How can I access the three member variables of ss through the pointer ptr?
Answer:
Ptr->;
Ptr-> B;
Ptr-> c;
How can I access the three member variables of ss through the pointer pstr?
Answer:
* Pstr; // access a, a member of the ss.
* (Pstr + 1); // access B, a member of ss.
* (Pstr + 2) // access the ss member c.
Haha, although I have adjusted the above Code on my MSVC ++ 6.0, It is not formal to use pstr to access structure members. To explain why it is not formal, let's take a look at how to access the various elements of the array through pointers:
Example 12:
Int array [3] = {35, 56, 37 };
Int * pa = array;
The method to access the three elements of the array through pointer pa is as follows:
* Pa; // access to unit 0th
* (Pa + 1); // access to unit 1st
* (Pa + 2); // access to unit 2nd
The format is the same as the format of the informal method used to access structure members through pointers.
When all C/C ++ compilers arrange the elements of an array, they always store each array unit in a continuous storage area. There is no gap between the Unit and the unit. However, when each member of the structure object is stored, in a compiling environment, it may need word alignment, double-word alignment, or other alignment, you need to add several "fill bytes" between two adjacent members, which may cause a gap of several bytes between each member.
Therefore, in example 12, even if * pstr accesses the first member variable a of the structure object ss, * (pstr + 1) cannot be guaranteed to access structure member B. Because there may be several Padding Bytes between member a and member B, maybe * (pstr + 1) will access these padding bytes. This also proves the flexibility of pointers. If your goal is to see whether there are any bytes filled with each structure member, hey, this is a good method.
The correct method for accessing structure members through pointers is to use the pointer ptr method in example 12.
Chapter 7. Relationship between pointers and functions
You can declare a pointer as a pointer to a function.
Int fun1 (char *, int );
Int (* pfun1) (char *, int );
Pfun1 = fun1;
....
....
Int a = (* pfun1) ("abcdefg", 7); // call a function through a function pointer.
You can use pointers as function parameters. In a function call statement, you can use a pointer expression as a real parameter.
Pointer array and array pointer
Haha, I am tired of explaining this.
Pointer array, the name of which is the pointer array. The elements of the array are pointers;
The array pointer is also the pointer to the directly thinking array.
Simple Example:
Int * p [2]; first, an array is declared. The elements of the array are int-type pointers.
Int (* p) [2]; declares a pointer pointing to an array with two int elements.
In fact, these two statements are mainly written because of the operator priority, because [] has a higher priority. So the first method is to combine p with [], so it is an array, followed by *, which is a pointer. The latter is written in the same way.
The processing of the pointer array is as follows:
Typedef int * intPtr;
IntPtr p [2];
To avoid confusion, it is also necessary to make proper typedef.
Likewise, array pointers can be processed similarly:
Typedef int intArray2 [2];
IntArray2 * p;
It is equivalent to the original statement.
I personally suggest using typedef for type definition during programming, so that the program looks much clearer.