算术运算:
指针+数字:需要调整,调整的权重为指针本身去掉一个*,求sizeof
指针-数字:需要调整,调整的权重为指针本身去掉一个*,求sizeof
指针+指针:非法,无意义
指针-指针:间隔的单元个数,需要调整,调整的权重为size of(指针去掉一个*)
例:int *p;
p+2的值为p的值加上2*sizeof(int)个字节
#include<stdio.h>
int main()
{
int *p=(int *)2000;
printf("%d\n",p-3);//1988
//p-3*sizeof(int)
printf("%d\n",(char*)p-2);//1998
//p-2*sizeof(char)
printf("%d\n",(float*)p-2);//1992
//p-2*sizeof(float)
printf("%d\n",(double*)p+2);//2016
//p+2*sizeof(double)
printf("%d\n",(short*)p+2);//2004
//p+2*sizeof(short)
printf("%d\n",(long*)p+2);//2008
//p+2*sizeof(long)
printf("%d\n",(unsigned long*)p-2);//1992
//p-2*sizeof(unsigned long)
printf("%d\n",(long long)p-2);//1998
//p-2
return 0;
}
指针-指针
#include <stdio.h>
int main()
{
int arr[10]={0};
int *p=&arr[1];//4(4*1)
int *q=&arr[7];//28(4*7)
printf("%d\n",p-q);//(4-28)/4=-6
printf("%d\n",(short*)q-(short*)p);//(28-4)/2=12
printf("%d\n",(long long*)q-(long long*)p);//(28-4)/8=3
printf("%d\n",(double*)q-(double*)p);//(28-4)/8=3
printf("%d\n",(unsigned long)q-(unsigned long)p);//28-4=24
printf("%d\n",(char**)q-(char**)p);
//(28-4)/4=6,去掉一个*后还是指针类型,为四个字节
return 0;
}
关系运算:
>, >=, <, <=, ==, !=