Why isn#39;t it legal to convert quot;pointer to pointer to non-constquot; to a quot;pointer to pointer to constquot;(为什么将“指向非 const 的指针转换为不合法?指向“指向 const 的指针)
问题描述
将指向非常量的指针转换为指向常数的指针是合法的.
It is legal to convert a pointer-to-non-const to a pointer-to-const.
那么为什么将指向非const的指针转换为指向const的指针是不合法的呢?
Then why isn't it legal to convert a pointer to pointer to non-const to a pointer to pointer to const?
例如,为什么下面的代码是非法的:
E.g., why is the following code illegal:
char *s1 = 0;
const char *s2 = s1; // OK...
char *a[MAX]; // aka char **
const char **ps = a; // error!
推荐答案
来自标准:
const char c = 'c';
char* pc;
const char** pcc = &pc; // not allowed
*pcc = &c;
*pc = 'C'; // would allow to modify a const object
这篇关于为什么将“指向非 const 的指针"转换为不合法?指向“指向 const 的指针"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么将“指向非 const 的指针"转换为不合法?指向“指向 const 的指针"
基础教程推荐
- [C语言]二叉搜索树 2023-09-07
- 全面了解C语言 static 关键字 2023-03-26
- C++实战之二进制数据处理与封装 2023-05-29
- C语言实现宾馆管理系统课程设计 2023-03-13
- C语言编程C++旋转字符操作串示例详解 2022-11-20
- C++实现ETW进行进程变动监控详解 2023-05-15
- [c语言-函数]不定量参数 2023-09-08
- C语言 详解字符串基础 2023-03-27
- 带你深度走入C语言取整以及4种函数 2022-09-17
- centos 7 vscode cmake 编译c++工程 2023-09-17
