Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/165.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在c++;,指定的字符是否已销毁? 我在C++中写了一个函数,它从字符数组中删除两个字符。我想当我把str[o+2]分配给str[o]时,str[o+2]不应该改变。但是当我使用cout打印它时,我看到str[o+2]被更改为null_C++_Arrays_String - Fatal编程技术网

为什么在c++;,指定的字符是否已销毁? 我在C++中写了一个函数,它从字符数组中删除两个字符。我想当我把str[o+2]分配给str[o]时,str[o+2]不应该改变。但是当我使用cout打印它时,我看到str[o+2]被更改为null

为什么在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++;,指定的字符是否已销毁?

我在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);

cout
abcdef0?…如果字符串末尾有空字符,您需要确保它不会被复制。请先尝试自己调试,这确实是一个非常简单的问题。一张纸和一支铅笔在这里非常有用。另外,请再次阅读初学者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)