例子1:
#include <stdio.h>
int main()
{
float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
float *ptr1 = &arr[0]; // ptr1 is 925601136
float *ptr2 = ptr1 + 3;// ptr2 is 925601148
printf("%f ", *ptr2);//90.500000
printf("%d", ptr2 – ptr1); //ptr2 - ptr1 is 3
return 0;
}
解释:
When we add a value x to a pointer p, the value of the resultant expression isp + x*sizeof(*p) where sizeof(*p) means size of data type pointed by p. That is why ptr2 is incremented to point to arr[3] in the above code.Same rule applies for subtraction. Note that only integral values can be added or subtracted from a pointer. We can also subtract or compare two pointers of same type.
例子2:
int main()
{