指针与指针的相减操作,表示两个指针指向的内存位置之间相隔多少个元素(注意是元素,并不是字节数)。
例如对于int类型的指针p和p1,p1-p的意义表示他们之间相隔多少个int类型的元素。
同样对于其他类型的指针变量之间相减的意义也是一样。
测试代码
#include <stdio.h>
int main(int argc,const char * argv[])
{
int a[3] = {1,2,3};
int *p = a;
printf("------------------p=%d,p++=%d\n", p, p++);
p = a;
while(p - a < 3)
{
printf("p=%d\n", p);
printf("p=%d\n", p+1);
printf("%d\n",*p ++);
}
return 0;
}