【C语言】指针+/-数字 && 指针-指针

//指针+/-数字

//指针+1 指的是加一个单元格,不同的指针类型需要调整,调整的权重为sizeof(去掉一个*)

//*** <注意:arr[i]==*(arr+i)> ***

//eg:
//double *dp=brr;
//dp++;//一个单元格是八个字节
//char *cp=crr;
//cp++;//一个单元格是一个字节
/*
#include <stdio.h>
int main()
{
int *p =(int *) 2000;//这里算野指针,只要不解引用程序不会崩溃,为了清楚了解指针+1,所以需要强转
printf("%d\n",p+4);// int 型 2000+4*4=2016
printf("%d\n",(char *)p+4);//2000+4*1
printf("%d\n",(short *)p+4);//2000+4*2
printf("%d\n",(double *)p+4);//2000+4*8
printf("%d\n",(float *)p+4);//2000+4*4
printf("%d\n",(long *)p+4);//2000+4*4
printf("%d\n",(unsigned short *)p+4);//2000+4*2
printf("%d\n",(unsigned long long)p+4);//无指针 ,是数字 2000+4=2004
printf("%d\n",(long *****)p+4);//指针类型都是四字节,2000+4*4(32位是四字节,64位是八字节)
return 0;
}
*/


//****只有  指针-指针  有意义
//1、先算出字节数 2、除以调整的权重
//总结:指针所有的运算都需要调整权重(sizeof 去掉一个*)
/*
#include <stdio.h>


int main()
{
int arr[10]={1,2,3};
int *p=&arr[1];
int *q=&arr[9];
printf("%d\n",p-q);//-8
printf("%d\n",q-p);//8
printf("%d\n",(short *)q-(short *)p);//32/2
printf("%d\n",(double *)q-(double *)p);//32/8
printf("%d\n",(int ***)q-(int ***)p);//32/4
printf("%d\n",(char **)q-(char **)p);//32/4
printf("%d\n",(long)q-(long)p);//32/1
return 0;
}
*/



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值