One, the movement of the pointer 1.1 pointer forward and backward movement
Each time the pointer is added , the pointer moves forward by the number of bytes that correspond to the pointer type . The following is an int pointer to an int array to see what the heck is the addition operation of a pointer?
intnums[]={ -, -, the, the, About}; int* ptr =Nums; printf ("%d\n",*ptr); PTR++;//ptr is a pointer of type int, so move backwards by 4 bytesprintf"%d\n",*ptr); PTR+=2;//Move backward 2*4 bytesprintf"%d\n", *ptr);
The results of the operation are as follows:
As you can see, the addition of a pointer moves forward the specified type of bytes, which in the array refers to the next element.
Let's look at the inverse subtraction with addition, still take the above code as an example, add a sentence: ptr--;
intnums[]={ -, -, the, the, About}; int* ptr =Nums; printf ("%d\n",*ptr); PTR++;//ptr is a pointer of type int, so move backwards by 4 bytesprintf"%d\n",*ptr); PTR+=2;//Move backward 2*4 bytesprintf"%d\n",*ptr); PTR--; printf ("%d\n", *ptr);
The results of the operation are as follows:
As you can see, subtraction represents the number of bytes that correspond to the pointer type being moved backwards .
1.2 The movement of a char type pointer
For pointer movement of char type, it actually refers to the next character or the previous character:
Char s1[]="hello edisonchou.cn"; Char* p=s1; P=p+2// char type occupies one byte, so here move backwards 2*1 bytes printf ("%s\n ", S1); printf ("%s\n", p);
An operation similar to a substring is implemented here, and the result is as follows:
1.3 Subtraction of the same type of pointer
Subtract from the same type of pointer, resulting in the length of the data type that is apart. The following is an example of the two int pointer to the same int array to verify the length of the data type being separated:
int nums[]={,---------; int* ip1=nums; int* ip2=nums; IP2=ip2+3// The same type of pointer subtraction is the length of the data type of the distance printf ("The Distance is%d\n", IP2-IP1);
When IP2-IP3 gets the distance is 3, this is because IP2 has moved forward the length of the 3 int type before the subtraction operation.
Second, the pointer is strong but dangerous 2.1 from your home to his home
Having just learned the power of pointer movement, now let's look at some of the "dangers" brought about by the powerful pointers. For example, in the following section of code, we define integers of two int types.
int i1=555 ; int i2=666 int * p=&I1; printf ( %d,%d\n ", &i1,&i2); P --; int i3=*p; // remove 4 bytes from the memory currently pointed to by P, interpreted as i3 printf ( %d\n , i3);
In the definition of pointer p, we point to I1. When we move the P pointer subtraction, we find that the current P pointer is not i1 but i2. This also means that when the pointer is pointing to the address of your home, and when the operation of the pointer to the next door to the old King's address, what the hell!
As can be seen from the run result graph, the addresses of I1 and I2 are 2686740 and 2686736 (consecutive addresses), and the P pointer starts at I1. When P moves backwards, the i2 is already pointed at this point. Therefore, the value of the output is 666.
2.2 Memory access out of bounds
Continuing with the above example, we then move the P pointer backwards to see what the value of the P pointer is at this point. When we move the P pointer backwards 99,999 bits, what is the value of the content it points to?
inti1=555; intI2=666; int* p=&I1; printf ("%d,%d\n",&i1,&i2); P--; intI3=*p;//Removes 4 bytes from the memory currently pointed to by P, interpreted as a i3printf"%d\n", i3); P--; printf ("%d\n", *p);
When you run the program, the result becomes:
So, what the hell is this 4200782 address? What content does it store? We do not see, this is another program inside a variable stored in the location, but now in our program actually through the pointer access to! It's a horrible thing! It's not safe to think that when a pointer in a program gets a memory address or data content in a B program by moving it, and then modifies the data with the pointer. Think of a bunch of hard-working programmers to work overtime to do the game, it is easy to hang up the plug, it would have to use RMB to buy points or props to the plug directly to change, is it not feel life has no meaning?
The following code shows that when the pointer moves too far away, the Windows system will restrict the access to the program, which directly causes an error.
p-=99999// This memory address may be inaccessible and is also referred to as access out of bounds //Windows has restricted access to this printf ("%d\n", *p);
The result of the run is that the direct crash, pop-up error button:
The appearance of the error box, which represents the harm of improper use of pointers, will cause the customer to delay the service for a certain period of time. Therefore, although the pointer is very powerful, but also very dangerous!
Resources
such as Peng Network, "C language can also do big Things (third edition)"
Zhou Xurong
Source: http://edisonchou.cnblogs.com
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.
You must know the pointer base-3. Pointer movement and risk of pointers