Reading Code Complete (and listening to Crockford's talks) has really opened my mind to writing clearer code constructs. For example, the for-loop's job in the first example should be to track indexes. There shouldn't be code that "does stuff" between parens. Instead of superficially breaking the for-loop into several lines and wasting time on aligning semicolons, it could be re-written as a while loop with clarity i…
Maybe the reason "C-hackers" do it that way is because it actually works, while your example completely fails to null-terminate the destination string.
while (*t++ = *f++)
;
This code really packs a lot of punch. The value of f is copied to t and then both pointers are incremented. If the value is 0 the loop is terminated. So there's always at least one character copied.In my initial rash response the loop would exit without copying the 0. So to fix it I might just add a new line
*to = 0;
(if I'm not mistaken the pointer is already incremented when the loop exits).Another option would be a while loop with a break statement, It looks weird, but does express the correct intent, which is "continuously copy from source string and exit if you've reached the end":
while (true) {
*to = *from;
if (*to == 0) break;
from++
to++;
}