- 1、本文档共6页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
const与指针(const与指针)
const与指针(const与指针)
const char*的pbuf;
*(pbuf + 1)= 0xf1;/ /这里编译问题“左值指定const对象”
定义的是个const指针,有什么可能的动作使系统认为试图修改一个项目类型的变量声明??
使用const修饰指针时,由于const的位置不同,而含意不同下面举两个例子,说明它们的区别。
下面定义的一个指向字符串的常量指针:
char *const PRT1 = stringprt1;
其中,ptr1是一个常量指针因此,下面赋值是非法的。
stringprt2 ptr1 =;
而下面的赋值是合法的:
* ptr1 =“M”;
因为指针PTR1所指向的变量是可以更新的,不可更新的是常量指针PTR1所指的方向(别的字符串)。
下面定义了一个指向字符串常量的指针:
const * PTR2 = stringprt1;
其中,是一个指向字符串常量的指针PTR2,所指向的字符串不能更新的PTR2,而PTR2因此是可以更新的,
* PTR2 =“X”;
是非法的,而:
stringptr2 PTR2 =;
是合法的。
所以,在使用const修饰指针时,应该注意const的位置。定义一个指向字符串的指针常量和定义一个指向字符串常量的指针时,const修饰符的位置不同,前者const放在*和指针名之间,后者const放在类型说明符前。
最后一句可能导致误会。澄清一下:
可以改变Pbus,但它指向的字符不可以通过它来改变。
例:
常量“*”=“常量字符串”;
中国共产党[ 0 ] = n ;//错,不能通过中共党指向的字符串来改变。
char =(char *)CCP;
CP [ 0 ] = n ;//可以。
const char*的pbuf意思是pbuf是指针,它指向的是常数字符。所以pbuf不是常数,但它指向的是常数。也就是说pbuf可以改变,但它指向的字符不可变。
常量整数;
常量PB;
常量计算机;
分别是指什么?
请简单举例,谢谢!
1、常量int *;
PA是一个指向int型常量的指针。
如:
常量= 5;
常量= 6;
常量;
* PA = 7;//错误,PA指向的是Na,Na是常量,所以无法附值
PA = Nb;/ /正确,PA可以指向其它int型的常量。
2、int *常量PB;
铅是一个指针常量,它指向一个int型的变量。
int=5;
int = 6;
常量;
* PA = 7;//正确,PA指向的变量可以改变。
PA = Nb;/ /错误,PA的指向不能改变。
3、常量计算机;
PC is a pointer constant that points to a constant of type int.
Const int nA=5;
Const int nB=6;
Const int *const pC=nA;
*pA=7; / / error, pA refers to nA, nA is a constant, so no value
PA=nB; / / error, pA point can not be changed.
1. Int is const, and PA can be modified.
2. Pb is const, and *pb, that is, int, can be modified.
3. Are const, can not be modified.
Const int *pA; pointer to const int; *pA cannot be changed. PA can
Int* const pB: points to the const pointer of int, pB cannot change, *pB can
Const int *const pC; the const pointer to const int, pC, *pC, cannot be changed
The const int *pA indicates that the address pointed to by *pA is fixed and can not be changed, for example:
PA = b; then pA=a is wrong
The int *const pB indicates that the address pointed to by pB can be changed, but the following address is a const, for example:
PB = a; pB = B; yes, but a = B; is wrong, because a is a co
您可能关注的文档
- 6消化与吸收生理(6 digestion and absorption physiology).doc
- 7 supporting science(7支持科学).doc
- 7 冠词(7 article).doc
- 6种方式搜索引擎营销期间可以利用假期和季节性(In the 6 way, you can use vacations and seasonality during the search engine marketing).doc
- 7-11便利店(7-11 convenience stores).doc
- 7360语音命令(7360 voice command).doc
- 73-丽水高三数学(73- Lishui senior high school mathematics).doc
- 75-第十二章 单元测试题(75- twelfth chapter unit test questions).doc
- 74ls248(74ls248).doc
- 6、《逍遥游》(节选)说课稿(6, free and unfettered travel (excerpt) lesson notes).doc
文档评论(0)