Devoted Devoted - 3 years ago 131
C Question

How does "while(*s++ = *t++)" copy a string?

My question is, what does this code do (from http://www.joelonsoftware.com/articles/CollegeAdvice.html):

while (*s++ = *t++);


the website says that the code above copies a string but I don't understand why...

does it have to do with pointers?

Answer Source

It is equivalent to this:

while (*t) {
    *s = *t;
    s++;
    t++;
}
*s = *t;

When the char that t points to is '\0', the while loop will terminate. Until then, it will copy the char that t is pointing to to the char that s is pointing to, then increment s and t to point to the next char in their arrays.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download