一、
指针+1是指加一个单元格
指针加减法都需调整 调整的是权重
权重为指针去掉一个星号,然后求sizeof()
example:
int*p 去掉* sizeof(int)为4;
double*p 去掉* sizeof(double)为8;
eg 1:(十进制 相加)
int *p=(int*)100;
printf("%d\n",p+4);//116
printf("%d\n",(char**)p+4);//116
printf("%d\n",(double*)p+4);//132
printf("%d\n",(unsigned long*)p+4);//116
printf("%d\n",(longlong*)p+4);//132
printf("%d\n",(longlong)p+4);//104
printf("%d\n",(short*)p+4);//108
printf("%d\n",(char*)p+4);//104
eg2: (十六进制 相减)
int*p=(int*)Ox2010;
printf("%x\n",p-2);//2008
printf("%x\n",(char***)p-2);//2008
printf("%x\n",(longlong*)p-2);//2000
printf("%x\n",(float*)p-2);//2008
printf("%x\n",(double**)p-2);//2008
printf("%x\n",(unsigned long)p-2);//200e
printf("%x\n",(char*)p-2);//200e
二、指针与指针相加减
1.指针+指针 非法无意义(地址加地址,相当于日期加日期,无任何意义)
2.指针+数字 地址加单元格 ( 另一个地址,相当于日期加天数,是另外一个日期,相减同理)
3.指针-指针 所间隔的单元格个数
单元格个数的算法:
①算出间隔的字节数(正负)
②除以权重
eg3:(指针与指针相减)
int arr[10]={0};
int * p=&arr[1];
int * q=&arr[9];
printf("%d\n",p-q) //-8
printf("%d\n",q-p) //8
printf("%d\n",(char**)p-(char**)q) //-8
printf("%d\n",(short*)p-(short*)q) //-16
printf("%d\n",(double*)q-(double*)p) //4
printf("%d\n",(longlong*)q-(longlong*)p) //4
printf("%d\n",(char*)q-(char*)p) //32
printf("%d\n",(longlong)q-(longlong)p) //32