成都网站建设顶呱呱,企业网站设计需要了解,网站建设专业工资,网站新闻前置备案文章目录一、指针是什么❔二、指针和指针类型1.指针-整数2.指针解引用三.野指针1.引起野指针的原因2.如果避免野指针完结一、指针是什么❔ 指针也就是 内存地址 #xff0c;在计算机上我们访问数据需要通过内存地址来访问#xff0c;在C语言中#xff0c;指针变量是用来存放…
文章目录一、指针是什么❔二、指针和指针类型1.指针-整数2.指针解引用三.野指针1.引起野指针的原因2.如果避免野指针完结一、指针是什么❔ 指针也就是 内存地址 在计算机上我们访问数据需要通过内存地址来访问在C语言中指针变量是用来存放内存地址的变量在不同系统下指针内存地址的长度不同32位CPU下由于有32根地址线所以指针内存地址是由32个bit位组成的也就是4Byte在64位CPU下有64根地址线所以地址由64个bit位组成也就是8Byte指针指向的地址都是一个内存单元一个内存单元里面有1byte的数据。 创建第一个指针变量
#include stdio.hint a 8; //先创建一个变量此变量在内存中有自己的地址。
int *pa a; //符号取出a的地址交给pa指针*符号表示pa是指针类型
*pa 10; //我们可以通过*pa修改a的值没有*的话pa是a的地址*是解引用的意思加上之后pa代表的就不是a的地址而是a
printf(%d,*pa); //10二、指针和指针类型
指针是用来存放地址的那么为什么还分为char* int* short* long*…….? 一般情况下char**类型的指针是为了存放char类型变量的地址int类型的指针是为了存放int类型的地址short类型的指针是为了存放short类型的地址…….但实际上指针的类型有一下两大作用 指针类型决定了指针进行±整数的时候±的步长字节指针类型决定了对指针进行解引用的时候能访问几个字节 1.指针±整数 代码示例:
#include stdio.hint main()
{int a 10;char* char_pa (char*)a;short* short_pa (short*)a;int* int_pa (int*)a;printf(%p\n,a); //00000071DCEFFC24 %p输出a的的地址printf(%p\n,char_pa); //00000071DCEFFC24 printf(%p\n,char_pa1); //00000071DCEFFC25 加了一个字节printf(%p\n,short_pa); //00000071DCEFFC24printf(%p\n,short_pa1); //00000071DCEFFC26 加了两个字节printf(%p\n,int_pa); //00000071DCEFFC24printf(%p\n,int_pa1); //00000071DCEFFC28 加了四个字节} 可以看到在地址1之前他们的地址都是与*a一样进行1之后char加了一个字节short加了两个字节int加了三个字节也就是说指针类型能决定指针±的时候可以±多少个字节±的字节由类型的长度决定。 2.指针解引用
代码示例
#include stdio.hint main()
{int a 0x11223344;char *char_pa (char*)a;short *short_pa (short*)a;int *int_pa a;printf(%d\n,*char_pa);printf(%d\n,*short_pa);printf(%d\n,*int_pa);}输出结果
明明我们赋值的都是a那为什么会造成三个类型解引用都不一样呢,我们可以用下面代码测试一下看看内存发生了什么。
int main()
{int a 0x11223344;char *char_pa (char*)a;*char_pa 0;
}这是在a没有改动之前 执行*char_pa 0;
a的一个字节被置为了0
int main()
{int a 0x11223344;short *short_pa (short*)a;*short_pa 0;
}执行*short_pa 0;
a的两个字节被置为了0 int main()
{int *int_pa a;*int_pa 0;return 0;
}执行*int_pa 0;
a的四个字节被置为了0
由此我们可以得出结论指针类型的第二大作用就是指针类型决定了对指针解引用时能访问或修改几个字节这由数据类型的大小决定
三.野指针 野指针会指向一段实际的内存但是野指针是指指针指向的位置是不可知的随机的不正确的没有初始化的没有明确限制的它指向哪里我们不知道或者说它指向的空间已经被我们释放那么他就是一个野指针在程序中我们必须要避免野指针的出现。 下面是容易出现野指针的场景
1.引起野指针的原因
1指针未初始化。
#include stdio.h
int main()
{int *p; //指针没有指向明确的地址那么将是随即地址也就是野指针*p 1;
}2指针越界访问
#inlcude stdio.h
int main()
{int arr[10] {0}int *p arr;for(int i0;i13;i){*(p) 8; //当指针超出arr数组的范围时p就是野指针}
}3指针指向的空间被释放
int main()
{int *p malloc(10 * sizeof(int));free(p);*p 10; //指针p指向的内存空间已经被释放此时的p就是野指针return 0;
}2.如果避免野指针
1指针初始化
2小心指针越界
3指针指向空间释放即使置NULL
4避免返回局部变量的地址
5指针使用之前检查有效性
int main()
{int *p NULL; //使用之前初始化为NULLif(p!NULL){*p 10; //使用之前检查有效性 }int *pa malloc(10 * sizeof(int));//此处省略n行代码free(p);pa NULL; //指针用完之后及时置空。
}完结
创作不易还请各位小伙伴多多点赞关注✨收藏⭐
文章转载自: http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.rszt.cn.gov.cn.rszt.cn http://www.morning.nj-ruike.cn.gov.cn.nj-ruike.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.bxfy.cn.gov.cn.bxfy.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.lxwjx.cn.gov.cn.lxwjx.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.nwllb.cn.gov.cn.nwllb.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.3dcb8231.cn.gov.cn.3dcb8231.cn http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.sqqpb.cn.gov.cn.sqqpb.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.lqlfj.cn.gov.cn.lqlfj.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.ndyrb.com.gov.cn.ndyrb.com http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.dqrhz.cn.gov.cn.dqrhz.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.c-ae.cn.gov.cn.c-ae.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.rtlg.cn.gov.cn.rtlg.cn http://www.morning.nfqyk.cn.gov.cn.nfqyk.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn http://www.morning.wmgjq.cn.gov.cn.wmgjq.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.lgtcg.cn.gov.cn.lgtcg.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.nnykz.cn.gov.cn.nnykz.cn http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn