C pointer (7) pointer operation,
(7) pointer operation
In the previous articles, we have seen the usage scenarios of pointer operations and used multiple pointer operations for verification. Here we will particularly summarize the essential meaning of pointer operations.
In C language, if p, pa, and pb are all pointer types, such operations are called pointer operations:
Its essence is 1 .. 2. The number of logical storage units instead of the number of physical bytes is obtained. Code Verification
int main(void){int array[] = {1, 2, 3};int *pa = array;int *pb = array + 3;printf("sizeof(pa)...%d\n", sizeof(pa));printf("pb-pa...%d\n", pb - pa);printf("((unsigned)pb-(unsigned)pa)/sizeof(pa)...%d\n", ((unsigned)pb - (unsigned)pa) / sizeof(pa));return 0;}
Run
Of course, it is also possible to interpret pa-pb as (char *) pa-(char *) pb)/sizeof (pa.
The pointer operation summarizes that the pointer stores the address, which is essentially an unsigned integer. For p + 1, it is not simply to move the value of p to a byte. One sentence:
Pointer operations are closely dependent on Pointer types.. Therefore, it is important to know the pointer type in Pointer operations. This is why pointer types can be tested using pointer operations.
It must be noted that operations like pa + pb do not exist!
Column Directory: C pointer
C language pointer auto-incrementing operation
# Include <stdio. h>
Main ()
{
Int a [] = {1, 2, 3, 4, 5 };
Int * p = NULL;
P =;
Printf ("% d,", * p); // p = a, fetch 1
Printf ("% d,", * (+ + p); // p = a + 1, get 2
Printf ("% d,", * ++ p); // p = a + 2, 3
Printf ("% d,", * (p --); // fetch 3 first, P-1 = a + 1
Printf ("% d,", * p ++); // extract 2 first, and then p + 1 = a + 2
Printf ("% d,", * p); // retrieve 3
Printf ("% d,", ++ (* p); // retrieve 3 + 1 = 4
Printf ("% d,", * p); // It is indeed 4, but I do not know why
Getch ();
}
, * (++ P) is not much different from * ++ p.
My Verification
% D, P value
1, 1231636
2, 1231640
3,1231644
3, 1231640
2, 1231644
3,1231644
4,1231644
4,1231644
Press any key to continue
Question about pointer addition and subtraction in C Language
First:
Float a [10], * p, * q;
P = & a [4], q = & a [7];
Q-p = 2 (there are two elements between a [4] and a [7]) *** this is incorrect. q-p = 3 is not equal to 2,
You can use printf ("% d", q-p.
P + 3-p = 12 is the location difference of the address in byte arrangement, indicating that there are 12 bytes between p + 3 and p.
Q-p = 3 indicates that there are three float data types between q and p, each of which occupies 4 bytes. 3x4 = 12 is consistent with the above result.