字符数组复制错误

Array of characters copy error

本文关键字:错误 复制 数组 字符      更新时间:2023-10-16

我的问题是,当内部for循环退出并进入外部for循环时,它停止向字符串指针pstrDestination添加字符。有人能给我解释一下吗,我没有终止字符数组所以它应该还是写的,不是吗?

// Does it match
if (strcmp(strCompareString, pstrToFind) == 0)
{
    // Reset the index of the found letter
    intFoundLetterIndex = (intFoundLetterIndex - intCompareIndex);
    // Add the characters from source to destination.
    for (intSourceIndex = 0; intSourceIndex < intSourceLength; intSourceIndex += 1)
    {
        pstrDestination[intDestinationIndex] = pstrSource[intSourceIndex];
        intDestinationIndex += 1;
        // Are we at the beginning of the target word
        if (intSourceIndex == intFoundLetterIndex)
        {
            // Add the replacement characters to the destination.
            for (intNewIndex = 0; intNewIndex < intReplaceWithLength; intNewIndex += 1)
            {
                pstrDestination[intDestinationIndex - 1] = pstrReplaceWith[intNewIndex];
                intDestinationIndex += 1;
            }
            intSourceIndex += intToFindLength;
        }
    }
}

我认为这

intDestinationIndex - 1;

应该像这样:

intDestinationIndex -= 1;

我能想到的最好的是,Visual Studio 2013 IDE正试图给我一个大大的拥抱。它为我终止了字符串。如果我将索引后退1,并将数组中的位置设置为' '。然后循环按预期执行,因为我重写了终止符。

pstrDestination[intDestinationIndex - 1] = ' ';