Sunday, March 9, 2014

String Copy

I've stumbled upon this piece of code earlier in the day and I have found it quite amusing.

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

Guess what it it does? It copies a string. It actually meant this.

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

Basically the process goes as follows, the contents of t (*t) are copied to s (*s) at every loop. Both s and t are then incremented with (++). And when (*t) couldn't get any further, it breaks out of the while loop. A string is then copied through that one lines of code, while being written in a much more compacted way.

By Jay Chua

No comments:

Post a Comment