Muh*_*man 2 c c++ string pointers immutability
我将非常感谢并感谢能够帮助我帮助我的他/她.它在C/C++中类似.
int string_length;
char *str;
str="Muhammad ashikuzzaman";
printf("%c",str[1]);
str[1]='o'; // Here an Unhandled Exception occurred
printf("%c",str[1]);
Run Code Online (Sandbox Code Playgroud)
在第6行或可能在5中发生未处理的异常并且输出未显示.我需要更改字符指针的任何特定字符.请帮我.
字符串文字在C和C++中是不可变的.也就是说,任何修改它们的尝试都会导致程序的未定义行为.而不是这些陈述
char *str;
str="Muhammad ashikuzzaman";
Run Code Online (Sandbox Code Playgroud)
用这个
char str[] = "Muhammad ashikuzzaman";
Run Code Online (Sandbox Code Playgroud)
至于你的指针str的定义和初始化,那么C++中的正确代码就是这样的
const char *str;
str="Muhammad ashikuzzaman";
Run Code Online (Sandbox Code Playgroud)
C++中的字符串文字具有常量字符数组的类型.