(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:
-
- , Premise: the PA and Pb types are the same.
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 pointer (7) pointer operation