指针的运算



//指针加法
//指针+1:加一个单元格
//指针+数字:需要调整,调整的权重为sizeof(指针去掉一个*)
//指针全部为4个字节(1级指针,2级指针。。。)
/*#include<stdio.h>
int main()
{
 int *p=(int *)2000;
 printf("%d\n",p+4);//2016 int型指针为4字节
 printf("%d\n",(short*)p+4);//2008去掉一个*为short型,2个字节
 printf("%d\n",(double*)p+4);//2032去掉一个*为double型,8个字节
 printf("%d\n",(float**)p+4);//2016去掉一个*为float型指针,为4个字节
 printf("%d\n",(unsigned short*)p+4 );//2008去掉一个*为unsigned short型,2字节
 printf("%d\n",(long*)p+4);//2016去掉一个*为long型,4字节
 printf("%d\n",(char*)p+4);//2004char型,1字节
 printf("%d\n",(unsigned long long)p+4);//2004常量,直接加4
 return 0;
}*/

//指针-数字:需要调整
/*#include<stdio.h>
int main()
{int *p=(int *)0x2010;
printf("%x\n",p-2);//2008
printf("%x\n",(short *)p-2);//200c 2010-4 因为是16进制数,借1当16减4等于12=c
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];
 int *q=&arr[9];
 printf("%d\n",p-q);//-8
 printf("%d\n",q-p);//8
 printf("%d\n",(short*)q-(short*)p);//16  用一开始定义的类型的字节数计算,除以调整以后的类型字节数
 printf("%d\n",(double*)q-(double*)p);//4  int型q-p=32 除以double型字节8==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 常量直接用int型做运算
}*/
//指针的所有运算都需要调整,调整的权重为sizeof

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值