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

尝试修改字符串时出现分段错误

发布时间:2020-12-16 10:50:43 所属栏目:百科 来源:网络整理
导读:参见英文答案 Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”?????????????????????????????????????17个 此代码在运行时给出了分段错误. char *str = "HELLO";str[0] = str[2]; 请有人能
参见英文答案 > Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”?????????????????????????????????????17个
此代码在运行时给出了分段错误.

char *str = "HELLO";
str[0] = str[2];

请有人能告诉我为什么吗?

解决方法

Standard不允许修改字符串文字.该字符串存储在程序的只读段中,例如在linux中,它存储在可执行文件的.rodata部分中,该部分无法写入.

(编辑:李大同)

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

    推荐文章
      热点阅读