How does this While loop work?

Hello, I was reading a c++ book for beginners, and i came across a program that takes two strings and 'tack' one to the end of another, i understood most of it, but there was one part (a loop) that i didn't understand

1
2
3
4
5
6
7
8
 void concatString(char* pszTarget, char* pszSource) //takes Source and tacks it 
                                                     //to the end of Target
{
    while(*pszTarget)
        pszTarget++; //find the end of pszTarget

    while(*pszTarget++ = *pszSource++); /*Here is the part i didn't understand*/
}

The while loop, according to the book, is suppose to copy pszSource to the end of pszTarget. What i don't understand is how the null character is also copied, and how (*pszTarget++ = *pszSource++) would eveluate to true or false, does the assignment operator return something?
Thanks in advance :D
In C++ each expression can have value that can be casted to bool. Everything except 0, '\0' and null evaluates to true. The three evaluae to false. Because of this, we can use various expressions as conditions for while loops.
It is important to remember, that assignment x = y is also an expression that evaluates to whatever value is being assigned, and that in turn can be cast to boolean and be either true (for anything) or false (for nothing, zero, null).

Consider the following case:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
char Text[200] = "I don't get it"; // The sentence has 15 letters 
                        // (so Text[0] to Text[14] 
                        //  are filled, and by C++ magic a '\0' 
                        //  character is put in Text[15])
                        //  Elements 16-199 are uninitialized.

char Source[200] = "Please, explain"; // As above


char * pszTarget = Text; // pointer points to Text[0];
char * pszSource = Source; // points to Source[0];

while(*pszTarget)        // Meaning untill pszTarget hits '\0', 0 or null
        pszTarget++;     // Point to next element 
                         // - goes through Text[0] to Text[15]

// At this point pszTarget points at Text[15]

while(*pszTarget++ = *pszSource++); 
// Assigns Text[15] value taken from Source[0]
// Repeats for Text[16] and Source[1] by magic of ++ operators, and so on
// When reaches Source[15] it turns out it is '\0', 
// so '\0' is assigned to Text[29]. 

// Now, since the value of expression (x = y) is y,
// the value of (*pszTarget = *pszSource]) can be 
// dereferenced as Text[31] = Source[15] - which is '\0' .
// And this is the breaking condition to the second while loop).  
Last edited on
Thank You! You explained it much better than the book did (in fact, the book didn't explain the loop at all, only said what it did)

Just to be 100% sure (sorry if annoying), the assignment is done BEFORE the entire expression is evaluated as true or false, right?

Thanks once again :D
Yeah, the assignment is done first, which returns the left-hand side (the thing that's being assigning to), and that gets casted to true or false.
Yes, the assignment is done before, and it works much like in normal school math - whatever is inside () is done first - which can be significant in some more obsucre expressions:
1
2
3
4
bool b;
int i = 0;
while( b = (i += add(12, 3)) < 1000)
    cout << b << ' ' << i << endl;
Last edited on
Is the ++ operators done if the while loop evaluates to false?
precedence is killing my eyes.
I don't ever want to work with strings and pointer operations again.
Last edited on

1
2
3
4
5
while(*pszTarget)       // If false
        pszTarget++;    // not done

while(*pszTarget++ = *pszSource++);  // Always done at least once, 
                                     //because it is a part of condition, which must be checked 

when the second while loop ends, does psztarget point to \0 or the next byte?
im sorry, c style strings and pointers were never my strong area.
When the second loop ends, pszTarget points to the last element of the target string, eg. Target[31] which can never have an address equal to \0, holds variable whose value is \0.
Topic archived. No new replies allowed.