首页 > 代码库 > c 指针兼容性问题
c 指针兼容性问题
指针兼容性问题:
- const指针不能赋值给非const指针.
- 非const指针可以赋值给const 指针,但前提是只是一层间接运算
1 Example: 2 int *pt1; 3 const *pt2; 4 const **pt3; 5 6 pt2=pt1;//OK 7 pt1=pt2;//NO 8 pt3=&pt2;//OK 9 pt3=&pt1;//NO double indirection10 Problem:11 pt1=pt2;//NO12 pt3=&pt2;//OK13 pt3=&pt1;//NO double indirection14 15 Explain:16 const n=5;17 Int *p1;18 Const int **p2;19 //consumption p2=&p1; is right20 p2=&p1;21 *p2=&n;//OK ,but this mean p1=&n;22 *p1=10;//OK because p1 is not const ,but that will be contradicted with consumption
指针兼容性问题: const指针不能赋值给非const指针. 非const指针可以赋值给const 指针,但前提是只是一层间接运算 Example: int *pt1; const *pt2; const **pt3; pt2=pt1;//OK pt1=pt2;//NO pt3=&pt2;//OK pt3=&pt1;//NO double indirection Problem: pt1=pt2;//NO pt3=&pt2;//OK pt3=&pt1;//NO double indirection Explain: const n=5; Int *p1; Const int **p2; //consumption p2=&p1; is right p2=&p1; *p2=&n;//OK ,but this mean p1=&n; *p1=10;//OK because p1 is not const ,but that will be contradicted with consumption
c 指针兼容性问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。