News for hackers. Stuff that matters.
Re:Lines of code that changed everything by minitech on Thursday November 28, 2019 @04:32AM (#21655813)
It’s not wrong, just not as short as it can be. Comparing things that aren’t booleans with zero explicitly is a common style choice.
Re:Lines of code that changed everything by imglorp on Thursday November 28, 2019 @09:43AM (#21657659)
Well, the article is about the historical context of the idiom, and they got the context wrong. The full context (K&R second edition at least) is working through pointers and trimming down strcpy(). The '\0' one is version 2, and the following is version 3.

    As the final abbreviation, observe that a comparison against
    '\0' is redundant, since the question is merely whether the
    expression is zero. So the function would likely be written
    as

    /* strcpy:  copy t to s; pointer version 3 */
    void strcpy(char *s, char *t)   {
       while (*s++ = *t++)
           ;
   }

   Although this may seem cryptic at first sight, the notational
   convenience is considerable, and the idiom should be mastered,
   because you will see it frequently in C programs.
https://rkvalley.files.wordpress.com/2010/04/kernighan_ritch... page 88
Re:Lines of code that changed everything by rstuart4133 on Thursday November 28, 2019 @05:54PM (#21660698)
> the notational convenience is considerable,

You are right of course - but this is a moment in time thing. ++ and it's equivalents aren't so convenient now. One of the things that strikes me about modern languages is the disappearance of integers, and the associated integer arithmetic such as the s++ in this example.

It turns out we were using integers as a poor mans iterator, and s++ simply meant "next item please". Once we starting using "for element in sequence" to access iterators many of the integers disappeared - they are only used to represent integer things in the real world now. Just about every instance of ++ also disappeared because "next item in iterator" was it's main use. Turns out integers modelling things in the real world rarely need incrementing. So much so that modern languages don't bother including ++ or the Pascal equivalent, succ().