指標的算術運算+(-)
//總結:指標的所有算術運算都需要調整,調整的權重為sizeof (指標去掉一個 * )
/*【指標+數字】√
【指標-數字】√
指標+1:加一個單元格
#include <stdio.h>
int main()
{
int*p = (int*)2000;
printf("%d\n",p+4);//2016
printf("%d\n",(short*)p+4);//2008
printf("%d\n",(double*)p+4);//2032
printf("%d\n",(float**)p+4);//2016
printf("%d\n",(unsigned short*)p+4);//2008
printf("%d\n",(long*)p+4);//2016
printf("%d\n",(char*)p+4);//2004
printf("%d\n",(unsigned long long)p+4);//2004 //注意此括號中無*
return 0;
}
#include <stdio.h>
int main()
{
int*p = (int*)0x2010;
printf("%x\n",p-2);//2008
printf("%x\n",(short*)p-2);//200c
printf("%x\n",(unsigned long*)p-2);//2008
printf("%x\n",(long long**)p-2);//2008
printf("%x\n",(float*)p-2);//2008
printf("%x\n",(double*)p-2);//2000
printf("%x\n",(char*)p-2);//200e
printf("%x\n",(unsigned long)p-2);//200e //注意此括號中無*
return 0;
}
*/
/*
【指標+指標】×
【指標-指標】√
//指標-指標:間隔的單元個數,不是位元組數
//做法:1、算出位元組數 2、除以調整的權重
#include <stdio.h>
int main()
{
int arr[10] = {1};
int *p = &arr[1];//x+4
int *q = &arr[9];//x+36
printf("%d\n",p - q);//[-(32/4)= -8]
printf("%d\n",q - p);//8
printf("%d\n",(short *)q - (short *)p);//16
printf("%d\n",(double *)q - (double *)p);//4
printf("%d\n",(int ***)q - (int ***)p);//8
printf("%d\n",(char **)q - (char **)p);//8
printf("%d\n",(long)q - (long)p);//32 //注意此括號中無*
return 0;
}
*/