Skip to content

Comment on Source Code Typographyparent

Comments

    for (; (*to = *from) != 0; ++from, ++to)
to and from are pointers to characters. When we increment them, we point to the next memory location in each string.
    "hello"  // a sequence of bytes with a zero marking the end 
     ^-- to
In our loop, we don't need to allocate any helper variables, and can just increment our pointers to point to the next memory slot in each string. I'm not a C expert, so I may get this subtly wrong, but I believe this is equivalent to:
    do {
        *to = *from;    // copy character from source to destination
    } while (0 != *to); // until we reach the '\0' terminator
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.