In the following program how or the two while statements working, while(szTarget[targetIndex]) & while(szTarget[targetIndex]) since the their first element is initialized to zero? Zero is false and one is true so how are they getting even one iteration?
The book I'M reading also made a point of saying this:

"It is very tempting to write C++ statements such as the following":

char dash[] = " - ";
concatString(dash, szMyName);

"This doesn't work because dash is provided just enough room to store four characters. The function will undoubtedly overrun the end of the dash array." <- What do the mean by that? I thought arrays were not changable anyway so how are they running lines like szTarget[targetIndex] = szSource[sourceIndex] in the first place, wouldn't this be editing the array szTarget[]?

Recommended Answers

All 5 Replies

In the following program

You forgot to include the following program.

What do the mean by that?

It means what it says. C++ doesn't stop you from walking off the end of an array:

int a[4];

// Don't do this
for (int i = 0; i < 100; i++) {
    a[i] = i;
}

This invokes what the standard calls undefined behavior, because it can corrupt adjacent memory or cause access violations if you try to access memory outside of your address space or memory that's protected in some way.

I thought arrays were not changable anyway

The size of an array is not changeable. The contents can be changed provided the array isn't specified as being read-only through the const keyword.

Concerning the first question, or what I take the first question to be, how does the statement while(szTarget[targetIndex]) work, or what is it doing.

That's actually a pretty common technique to use when writing your own functions to manipulate c-style strings (array based, null terminated). The loop keeps examining/processing characters of the string until the null is encountered.

Here's a simple example of how the strcpy( ) function might be written

void str_cpy( char dest[], const char src[] )
{
    int index = 0;

    while( src[index] )  //while it's not the NULL terminator, or value 0
    {
        dest[index] = src[index]; //copy the character
        index++;
    }
    src[index] = '\0'; //don't forget to add the null terminator
}

Keep in mind that any non-zero value has the logical value of true, so the loop continues as long as there are any characters in the source string. The null terminator, shown with the escape code \0, has the ASCII value 0 (zero).

@ vmanes Shouldnt line 10 of your snippet be

dest[index] = '\0';  // null terminate the destination string

NathanOkiver, I thought so too but that's the way my book and in down. The weird thing is several lines later when the paragraph was talking about the previous code it referenced that piece of code as being \0. But the program worked anyway so what the hell. Thanks everyone, it's clearer now.

Nathan, oops, yes, I made a itty bitty boo boo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.