gpt4 book ai didi

c++ - 我自己的连接函数不会更改目标字符串

转载 作者:行者123 更新时间:2023-11-28 06:54:02 24 4
gpt4 key购买 nike

我试过调试这段代码,但即使我在 while 循环中循环遍历字符串,看起来我也没有循环任何东西。函数调用后目标数组不变。

void stringConcatenate(char * destination, char * source)
{
int index1 = 0; //for cycling subscripts
int index2 = 0;

while (destination[index1] != '\0') //cycle to end of first string
{
index1++;
}
index1++; //to get to null character
while (source[index2] != '\0') //cycle through second string appending along the way
{
destination[index1] = source[index2];
index1++;
index2++;
}
destination[index1] = '\0'; //append null point

最佳答案

在第一个 while 语句之后增加 index1 的行不应该存在。

index1++; //to get to null character <-- incorrect.

index 已经指向终止符。通过在那里递增它,您最终会在终止符 之后附加一个字符。

关于c++ - 我自己的连接函数不会更改目标字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23400878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com