When I was writing a program recently, I found myself forgetting a lot of things. Today, I finally have the opportunity to make a summary of the pointer, and I hope it will be useful as a note. If something is incorrect, I hope you can help me correct it. Then, my experiment environment is 32-bit RHEL + eclipse.
I. Basic pointer attributes
Pointer attributes mainly include pointer types, pointer types, and pointer values. The following uses a simple example as an example.
Int * p;
Pointer type: int *
Type pointed to by pointer: int
Pointer value (the address of the pointer pointing to the memory area): A wild pointer
To sum up, pointer type: the part after the variable name is removed
Type pointed to by pointer: remove the part after * variable name
Pointer value: the address pointed to by the pointer to the memory zone.
At last, there is a small dot, indicating whether the memory space is occupied. The answer is yes. Let's do a simple experiment and test it:
Why are they all 4 bytes? Because our pointer storage content is the memory address, and my host is 32 bits. The 32-bit value is 4 bytes!
Note: Distinguishing pointer types and pointer types is the key to proficient in C.
Ii. & and * Operators
& Operator is called the address-taking operator, and * operator is called the indirect operator (the content-taking operator ). When they are combined with pointers, they often bring us a headache. Image Segment errors are often caused by improper use. Here is a better summary:
The operation result of & p is a pointer, And the pointer type is p Plus *. The Pointer Points to the p type, and the point is the p1 address.
* P's calculation result is what p points to. Its type is p's type, and its address is p's address.
The theory is hard to understand. Let's do the experiment:
The operation result of & n is a pointer. the pointer type is int *, and the pointer type is int. (Match the p information on the left of the equal sign)
The operation result of & p is a pointer. the pointer type is int **, And the pointer type is int *. (Match the ptr information on the left of the equal sign)
Printf ("The value of p is % d \ n", * p );
Since the previous p = & n, p points to the n address. However, & p (p's own address) remains unchanged. So * the value of p is the content of the address pointed to by p, that is, 4. * P is of the int type, so % d is used for output here.
Printf ("The value of ptr is % x \ n", * ptr );
Similarly, ptr points to the p address. However, & ptr (ptr's own address) remains unchanged. Therefore, * the ptr value is the content of the address (p address) pointed to by ptr, that is, the address of n. * The ptr type is int *, so % x is used for output here.
To put it more simply, * is the drop * operator, and "&" is the litre * operator.
3. pointers and Arrays
With the above foundation, let's continue to look at pointers and arrays. Let's take a look at the following test code:
First, we find that for an array like type name [n];, we can use * (name), * (name + 1), * (name + 2 )...... Recursively accesses every element of the array.
Note: we need to add brackets to access the program. The above and we see that many programs do not contain brackets. We will introduce their specific differences below.As we all know, an array is a continuous memory block, and the name pointer stores the starting address of the array memory block. So here we get the content in the starting address through the indirect operator * (take the content operator) in section 2. Then, access the content on the second address with + 1. However, it should be noted that the numbers such as 1 here are not int-type numbers in our understanding. The number here is the length of the type. The preceding int array [10] is used as an example. The int length is 32 characters (4 bytes ). Therefore, * array + 1 refers to the starting address + 4 bytes. That is, the storage address of array [1. Next, let's try to make The string array as complex as 1.1: printf ("The second char of % s is % c. \ n ", * team, * (* team + 1 ));
This is an array pointer, which is a little more complex than before. Because the C language does not have the string type, China is actually a char array. The team array here is a pointer of the 3 character type. The team stores the starting address of the three character arrays (China, American, and Japan), that is, the starting address of China. Therefore, the first parameter * team obtains the string China.
What should we do if we want to access the specified characters in a special string through the team? First, we need to distinguish the following concepts:
* Difference between team + 1 and * (team + 1)The two ones here are different for the compiler. Why? As mentioned above, the number here is the type length. However, the type length of * (team + 1) is sizeof (char *) 4 bytes, And the type length of * team + 1 is sizeof (char) 1 byte. Why is this? We will introduce it in the following step change rules!
So why can I access array [2] Through * array + 1?The main reason is that the 1 compiler in * (array + 1) considers it to be 4 bytes (sizeof (int), while the 1 compiler in * array + 1 considers it to be an int integer 1. So when the array is {0, 4, 3, 6, 7}, * array + 1 will get the answer 1 instead of 4.
What are the rules for changing the step size?
To understand the variation of step size, first we need to clearly obtain the element type of the array. In fact, it is also very simple, that is, remove the remaining variable names
Is the array element type:
Char * array [10]; // The element type is char *
Int test [10]; // The element type is int.
So what is the type of the above-level element is actually removing the element type *. If no * exists, it indicates that it is already top-level.
Char * array [10]; // The element type is char *, and the parent element is char
Int test [10]; // The element type is int, top-level
The last step is the change rule:
1) when the variable name is combined. The length of the step to the upper-level element.
2) When the top-level element is combined with *, the type of step size to is the regular int.
3) The initial step size is the length of the array element type.
Therefore, the step size of * (array + 1) is sizeof (char *) // because it is not combined with *, The step size here is sizeof (char *)
* The step size of array + 1 is sizeof (char) // because it is combined once, the step size here is the length of the char of the superior element type.
* (Test + 1) The step size is sizeof (int) // because it is not combined with *, The step size is sizeof (int)
* The step of test + 1 is an integer of 1. That is to say, if * test value is 4, the value of * test + 1 is 5. // After combining, int is already a top-level element. You cannot upgrade it any more, so the compiler uses it as an integer by default.
4. pointer and struct
It's still the old rule. Let's dry the code before analyzing it:
Here we should first clarify some concepts, and use the pointer to access the internal changes of the struct through->. The struct itself uses.To access your own variables.
Therefore, p points to The newly created struct and uses printf ("The content of p is % d and % x. \ n ", p-> data, p-> next); to access the internal variables of your own struct.
What is more complicated here is:
Printf ("The content of n is % d and % x. \ n ", (* (p-> next )). data, (* (p-> next )). next );
Here p-> next points to the n storage address, and then we use * to retrieve n content. Then, you can use. To access n's own variables. Do you feel familiar with pointers? O (partition _ partition) O
5. Forced type conversion of pointers
To find out why we can do this, we need to first understand the following points:
1) Structure
typedef struct node{ int data; struct node *next;};
Taking the struct here as an example, the int type is 4 bytes. Struct node * Is pointer type, so it is 4 bytes. Therefore, the first output is 8.
2) Step after forced type conversion
After the forced type conversion, the step size also changes. Because the starting element type (the pointer pointing to the variable type in a module) is converted to int, the step here is sizeof (int), which is 4 bytes.
Printf ("The value of n is % d and % x. \ n", * p, * (p + 1 ));
Note that the step of the first int variable of struct node is also 4 bytes. Therefore, the starting address after (p + 1) is offset by 4 bytes. Then the starting address of the variable * next is obtained, and its content is retrieved through.
This is the end! I hope you will become a master of C language soon. If there are any errors on it, or you may have any questions after reading it. Everyone is welcome to point it out and discuss it together!
Reprinted Please note: http://blog.csdn.net/grublinux/article/details/28648377