c - 为什么我不能修改 c 中的字符串文字?

标签 c

我试图理解无法在 C 中修改字符串文字的原因。

为什么以下在 C 中是非法的?

char* p = "abc";
*p = 'd';

最佳答案

来自C89 Rationale, 3.1.4 String literals :

String literals are specified to be unmodifiable. This specification allows implementations to share copies of strings with identical text, to place string literals in read-only memory, and perform certain optimizations. However, string literals do not have the type array of const char, in order to avoid the problems of pointer type checking, particularly with library functions, since assigning a pointer to const char to a plain pointer to char is not valid. Those members of the Committee who insisted that string literals should be modifiable were content to have this practice designated a common extension (see F.5.5).

关于c - 为什么我不能修改 c 中的字符串文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58584310/

相关文章:

c - Linux 中的 Fifos 数据包模式

c - 如何杀死多个子进程

android - 尝试编译 native 代码时出现权限问题

c - A a = (A) b; 中是否需要显式类型转换?

c - 使用 OpenMP 在两个内核上设置线程关联

c - 从 GCC 获取函数十六进制代码

c++ - Emacs 24.3.1、Ubuntu 14.04.1 LTS、emacs 窗口切换中的 gdb 都崩溃了

受控嵌套循环

c - C 中两个数组元素地址之间的偏移量

c - 我需要 GCC 的 -pedantic 标志和 C11 吗?