- #include <stdio.h>
-
-
int main(int argc, char *argv[])
-
{
-
int urn[5]={100,200,300,400,500};
-
int *ptr1,*ptr2,*ptr3;
-
-
ptr1=urn;
-
ptr2=&urn[2];
-
printf("ptr1=%p,*ptr1=%d,&ptr1=%p\n",
-
ptr1,*ptr1,&ptr1);
-
ptr3=ptr1+4;
-
printf("ptr1+4=%p,*(ptr4+3)=%d\n",
-
ptr1+4,*(ptr1+3));
-
ptr1++;
-
printf("ptr1=%p,*ptr1=%d,&ptr1=%p\n",
-
ptr1,*ptr1,&ptr1);
-
-
ptr2--;
-
printf("ptr2=%p,*ptr2=%d,&ptr2=%p\n",
-
ptr2,*ptr2,&ptr2);
-
-
--ptr1;
-
++ptr2;
-
-
printf("ptr2=%p,ptr1=%p,ptr2-ptr1=%d\n",
-
ptr2,ptr1,ptr2-ptr1);
-
-
printf("ptr3=%p,ptr3-2=%p\n",
-
ptr3,ptr3-2);
-
-
getchar();
-
-
return 0;
-
}
运行结果:
总结:
两指针相减为两指针相隔的存储单元个数,每个存储单元大小为sizeof(指针类型)Byte
指针减整数为指针指向的地址向后退 整数×sizeof(指针类型)Byte