更改字符串的指针,通过字面用C? [英] Change string literal in C through pointer?
本文介绍了更改字符串的指针,通过字面用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string\">Why做写入字符串时,我得到一个分段错误?
我有以下程序:
char *s = "abcdf";
char s1[50] = "abcdf";
s1[0] = 'Q'; // Line 1
s[0] = 'P'; // Line 2
为什么 1号线
工作正确和 2号线
导致程序崩溃?
Why Line 1
worked correctly and Line 2
caused the program to crash?
推荐答案
2号线点是在第1行只读,而你的可执行文件的数据段,程序初始化 S1
阵列给定的字符串。这是存储在堆栈中,您可以修改。
Line 2 points to the data section of your executable which is read-only, whereas in line 1, the program initializes s1
array with the given string. This is stored in stack, which you can modify.
这篇关于更改字符串的指针,通过字面用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文