为什么在c++;,指定的字符是否已销毁? 我在C++中写了一个函数,它从字符数组中删除两个字符。我想当我把str[o+2]分配给str[o]时,str[o+2]不应该改变。但是当我使用cout打印它时,我看到str[o+2]被更改为null
为什么在c++;,指定的字符是否已销毁?为什么在c++;,指定的字符是否已销毁? 我在C++中写了一个函数,它从字符数组中删除两个字符。我想当我把str[o+2]分配给str[o]时,str[o+2]不应该改变。但是当我使用cout打印它时,我看到str[o+2]被更改为null,c++,arrays,string,C++,Arrays,String,为什么在c++;,指定的字符是否已销毁? 我在C++中写了一个函数,它从字符数组中删除两个字符。我想当我把str[o+2]分配给str[o]时,str[o+2]不应该改变。但是当我使用cout打印它时,我看到str[o+2]被更改为null #include<iostream> #include<string.h> using namespace std; void shiftLeft(char*,int, int); int main(){ ch
我在C++中写了一个函数,它从字符数组中删除两个字符。我想当我把
str[o+2]
分配给str[o]
时,str[o+2]
不应该改变。但是当我使用cout打印它时,我看到str[o+2]
被更改为null
#include<iostream>
#include<string.h>
using namespace std;
void shiftLeft(char*,int, int);
int main(){
char str[100];
cout<<"enter the string: ";
cin>>str;
cout<<"removes the letter with index i and i+1\nenter i:";
int i;
cin>>i;
int n=strlen(str);
shiftLeft(str,i,n);
cout<<str;
return 0;
}
void shiftLeft(char*str,int i, int n){
for(int o=i-1; o<n; o++){
str[o]=str[o+2];
}
}
#包括
#包括
使用名称空间std;
void shiftLeft(字符*,整数,整数);
int main(){
char-str[100];
库斯特;
库蒂;
int n=strlen(str);
shiftLeft(str,i,n);
coutabcdef0?…如果字符串末尾有空字符,您需要确保它不会被复制。请先尝试自己调试,这确实是一个非常简单的问题。一张纸和一支铅笔在这里非常有用。另外,请再次阅读初学者C教科书中有关字符串的章节。这也是一个非常简单的问题n定义的行为。在条件o
下,变量o
最多获取n-1
,访问str[o+2]
然后等于str[n+1]
,超出og界限。
abcdef0???... <- the contents of array (0 means NUL, ? means indeterminate)
ef0? <- what is written to the position (shifted 2 characters)