Adam Schneider
Adam Schneider

Reputation: 295

Array of characters copy error

My question is this, when the inner for loop exits, and enters back into the outer for loop it stops adding characters to the string pointer pstrDestination. Can some one explain that to me, I am not terminating the array of character so it should still write, shouldn't it?

// 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;
        }

    }

}

Upvotes: 0

Views: 45

Answers (2)

Adam Schneider
Adam Schneider

Reputation: 295

Best I can come up with is, The Visual Studio 2013 IDE is trying to give me a huge big ol' hug. It is terminating the string for me. if I step the index back 1 and set the spot in the array equal to ' '. Then the loop executes as expected cause I over wrote the terminator.

after the inner loop I added;

pstrDestination[intDestinationIndex - 1] = ' ';

Upvotes: 0

Estiny
Estiny

Reputation: 888

I think this

intDestinationIndex - 1;

should look like this:

intDestinationIndex -= 1;

Upvotes: 2

Related Questions