. * (P + 4) = P [4] = A [4] = * (a + 4). This is the final result. Note that the character pointer P and array name a each have two reference methods, one is the array reference method, such as a [4], one is the * (a + 4) pointer method.
Printf ("% s \ n", P); the printed content is still[HTML]View plaincopyprint?
- Brucelee!
BruceLee!
[HTML]
View plaincopyprint?
That is to say, we only use * (p + 4) to view the content, but it does not change the point of P. P still refers to the first address, so the printed string starts with the first character.
If we add the following two sentences to the Code:
Printf ("% C \ n", * P ++ );
Printf ("% s \ n", P );
The first execution result is to print the first character 'B', and then P moves back one character. Therefore, the following print string starts with the second character and returns rucelee.
CONCLUSION: (* P + 4) This method does not change the point of pointer p. Only when P ++, p --, or P + = 4 is similar can the pointer P be changed!
Here is an example:
[CPP]
View plaincopyprint?
- <Span style = "font-size: 18px"> # include <stdio. h>
- Void main ()
- {
- Int A [5] = {1, 2, 3, 4, 5 };
- Int * P =;
- Printf ("% d \ n", a [0]);
- Printf ("% d \ n", * P );
- Printf ("% d \ n", P );
- Int * q = (int *);
- Printf ("% d \ n", Q [0]);
- } </Span>
#include <stdio.h>void main(){ int a[5] = {1, 2 ,3 , 4, 5}; int *p = a; printf("%d\n", a[0]); printf("%d\n", *p); printf("%d\n", p); int *q = (int *)a; printf("%d\n", q[0]); }
There are two points to be mentioned:
1,
[CPP]
View plaincopyprint?
- <Span style = "font-size: 18px"> printf ("% d \ n", P); </span>
printf("%d\n", p);
This statement cannot be executed normally, and no numbers related to array a are printed.Do not expect a head address like printf ("% s", P) p. This method is used to print the content of the entire array!
2, int * P = A; and int * q = (int *) A. The two statements have the same effect. The preceding mandatory conversions are the same.
Let's look at this confusing example:
[CPP]
View plaincopyprint?
- # Include <stdio. h>
- Void main ()
- {
- Int A [5] = {1, 2, 3, 4, 5 };
- Int * m = (int *) & A [0];
- Printf ("% d \ n", * (m + 1 ));
- Int * P = (int *) &;
- Printf ("% d \ n", * (p + 1 ));
- Int * q = (int *) (& A + 1 );
- Printf ("% d \ n", * (q-1 ));
- Int * w = (int *) (& A [0] + 1 );
- Printf ("% d \ n", * (W-1 ));
- }
#include <stdio.h>void main(){ int a[5] = {1, 2 ,3 , 4, 5}; int *m = (int *)&a[0]; printf("%d\n", *(m + 1)); int *p = (int *)&a; printf("%d\n", *(p + 1)); int *q = (int *)(&a + 1); printf("%d\n", *(q-1)); int *w = (int *)(&a[0] + 1); printf("%d\n", *(w-1)); }
There are three key points:
1. Note that (int *) is dispensable, but whenAfter & is added, in int * m = (int *) & A [0], if the conversion is not enhanced, an alarm is triggered. Because the address is obtained here, it is best to force conversion!
2, (int *) (& A [0]) and (int *) (& A), in terms of printing m and P values, it seems that there is no difference between the two operations, but this is not the case! & A [0] = A indicates the first address of array A, that is, the address of a [0. But & A is the whole object, that is, the whole first address of the array. Which of the following is the address of the pointer Q?
We must remember to add 1 to the pointer to get the address of the next element, rather than directly adding 1 to the value of the original address. We certainly know this. If the type is X, after 1 is added, the pointer moves sizeof (x) backward, and the movement is in the unit of sizeof (X!
The more I say it, the less I understand it. It is actually the difference between a and a [0] And &!
The first two are equivalent. & A as mentioned above, regards A as a whole, so & A + 1 is the address of the next object of A, that is, & A + 1, relative to a or & A [0], the sizeof (A) = 5*4 = 20 bytes are moved. Here, the pointer Q points to a [5]! Therefore, the value of * (Q-1) is 5, that is, the value of a [4. In order to compare the differences, I finally took (& A [0] +
1) For comparison, W points to a [1] in the declaration, * (q-1) value is 1, that is, a [0] value.
3,Int * n = A is equivalent to int * n = (int *) & A [0]. int * n = (int *) &;
But when there is a pointer addition or subtraction operation, the results of the two are never the same!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!
For example, int * n = (int *) (& A [0] + 1) is never equal to int * n = (int *) (& A + 1 ).
Http://blog.csdn.net/yanzi1225627/article/details/7858231