加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – const指向不符合预期的指针

发布时间:2020-12-16 03:27:18 所属栏目:百科 来源:网络整理
导读:运行时为什么会出错?我期待ptr_ref无法修改ptr指向的地址,但事情似乎没有按计划进行. int b = 3;int* ptr = b;//says something about cannot convert int* to type const int*const int* ptr_ref = ptr; 提前致谢, 15岁的C noob 解决方法 声明ptr_ref不是
运行时为什么会出错?我期待ptr_ref无法修改ptr指向的地址,但事情似乎没有按计划进行.
int b = 3;
int* ptr = &b;
//says something about cannot convert int* to type const int*&
const int*& ptr_ref = ptr;

提前致谢,
15岁的C noob

解决方法

声明ptr_ref不是指向int的指针的const引用,而是对指向const int的指针的引用,因此您的类型不匹配.你必须这样做
int* const& ptr_ref = ptr;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读