gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:10:13 25 4
gpt4 key购买 nike

我试图理解无法在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com