我用各种各样的C教程和与指针相关的书籍一直在苦苦挣扎几个小时,但我真正想知道的是,如果可以在创建一个char指针后更改它.
这是我尝试过的:
char *a = "This is a string";
char *b = "new string";
a[2] = b[1]; // Causes a segment fault
*b[2] = b[1]; // This almost seems like it would work but the compiler throws an error.
Run Code Online (Sandbox Code Playgroud)
那么有没有办法改变字符串内的值而不是指针地址?
谢谢
编辑:
谢谢大家的回答.现在更有意义了.特别有意义的是,为什么有时它工作正常而其他时候不工作.因为有时候我会传递一个char指针,有时会传递一个char数组(char数组工作正常).