Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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+中的数组连接+;再一次_C++ - Fatal编程技术网

C++ 与C+中的数组连接+;再一次

C++ 与C+中的数组连接+;再一次,c++,C++,我仍然有这个代码的问题。测试台说它花费的时间太长并且崩溃 编辑3: void mystrcat(char destination[], const char source[]){ int counter = 0; int counter2 = 0; while(destination[counter2] != '/0'){ counter2++; } while(source[counter] != '\0'){ dest

我仍然有这个代码的问题。测试台说它花费的时间太长并且崩溃

编辑3:

void mystrcat(char destination[], const char source[]){
    int counter = 0;
    int counter2 = 0;
    while(destination[counter2] != '/0'){
        counter2++;
    }

    while(source[counter] != '\0'){
        destination[counter2 - 1 + counter] = source[counter];
        counter++;
    }
    destination[counter] += '\0';
}

int main(){

}
#包括
使用名称空间std;
void mystrcat(字符目的地[],常量字符源[]){
int计数器=0;
int计数器2=0;
while(目标[counter2]!='\0'){
计数器2++;
}
while(源[计数器]!='\0'){
目的地[计数器2+计数器]=源[计数器];
计数器++;
}
目的地[计数器2+计数器]+='\0';
}
int main(){
}
它仍然说它正在产生一个错误的结果

#include <iostream>
using namespace std;

void mystrcat(char destination[], const char source[]){
    int counter = 0;
    int counter2 = 0;
    while(destination[counter2] != '\0'){
        counter2++;
    }

    while(source[counter] != '\0'){
        destination[counter2 + counter] = source[counter];
        counter++;
    }
    destination[counter2 + counter] += '\0';

}

int main(){
}
在此循环中使用
计数器
计数器2
。否则它永远不会结束

我想你需要

while(destination[counter] != '/0'){
    counter2++;
}
EDIT2

刚刚注意到-您需要“\0”而不是“/0”

while(destination[counter2] != '\0'){
    counter2++;
}


假设目的地足够大,就不会崩溃。至于它花费的时间太长,我不明白为什么会这样,除非你在非常严格的时间安排下。

修改后,它仍然说它花费的时间太长并且崩溃。@user3024476-请不要像那样编辑问题-答案/注释不再有意义。只需附加一个更新。
while(目的地[counter2]!='/0'){
应该是
while(目的地[counter2]!='\0'){
现在测试工具说它给出了一个错误的结果,但至少是一个开始。@user3024476-获得一份打印件和一张纸,然后依次完成每个步骤(或使用调试器)我编辑了我的代码,但它仍然说它给出了一个不正确的结果。但不确定问题出在哪里。可能是+='\0';删除+='并将其替换为'='。它终于起作用了。谢谢大家的帮助。我现在正在研究这个问题。为什么
destination[counter2+counter]+='\0';
当它应该是
目的地[counter2+计数器]='\0';
目的地[counter]+='\0';
不分配
'\0'
(或
NULL
),也不更改
目的地[counter]
的值。因此,除非
目的地[counter]
NULL
,您的字符串很可能不会指向任何地方。在这种情况下,如果
destination
数组的其余部分没有任何
NULL
字符,则将其作为字符串访问需要很长时间,因为它在
destination
数组的边界外的索引处查找
NULL
字符,但没有找到然而,否则,你的应用程序可能会因为修改这些索引而崩溃。
while(destination[counter2] != '\0'){
    counter2++;
}
//Make sure the destination is big enough to hold source!

void mystrcat(char destination[], const char source[]){
    int counter = 0;
    int counter2 = 0;
    while(destination[counter2] != '\0') counter2++;
    while(source[counter] != '\0') counter++;

    memmove(destination+counter2, source, counter);
    destination[counter+counter2] = '\0';
}
void mystrcat(char destination[], const char source[]){
    strcat(destination, source);
}
void mystrcat(char destination[], const char source[]){
    int counter = 0;
    int counter2 = 0;

    // This loop looks like it will work. We will exit when destination[counter2] ==   '\0'. Just change '/0' to '\0'.
    while(destination[counter2] != '/0'){
        counter2++;                                 
    }

    // I think you should change destination[counter2 - 1 + counter], to destination[counter2 + counter]. That way you overwrite the null char in destination, as you would want.
    while(source[counter] != '\0'){
        destination[counter2 - 1 + counter] = source[counter];
        counter++;
    }

    // Uhh, change this to destination[counter + counter2] = '\0'.
    destination[counter] += '\0';
}