Alternatives to modifying loop var in the loop.

Discussion in 'C Programming' started by Matt, Dec 27, 2013.

  1. An assignment yields the value that was assigned. That value
    is the condition tested by the "while".

    It's a bit terse for my own taste, but it's well defined and a common C
    Keith Thompson, Jan 2, 2014
    1. Advertisements

  2. Matt

    James Kuyper Guest

    If you need the value of either s or d after the loop has executed,
    which is not a particularly uncommon need, the loop is better than a
    call to strcpy(), and some modern compilers can optimize it almost as
    much as strcpy() itself.
    I've met quite a few C programmers who couldn't answer that question,
    and most of the ones who could were unable to explain why it works.
    True: dropping an '=' is easier than accidentally typing strcmp() when
    you meant strcpy() (but I have in fact seen that done).
    I only get one warning, about the use of = in a context where == might
    have been meant - what's the other warning?
    I have some sympathy with the idea of treating warnings as if they were
    errors, but a certain amount of judgement is needed, too. A compiler is
    free to warn about anything it wants - for instance, the use of a
    obscene word as an identifier (or even the failure to use obscene words
    as identifiers).
    Agreed - even if the co-worker is in fact a moron, there's better ways
    to deal with that fact.
    James Kuyper, Jan 2, 2014
    1. Advertisements

  3. Matt

    James Kuyper Guest

    "An assignment expression has the value of the left operand after the
    assignment" (6.5.16p1); since that value is 0, it will terminate. That
    seems perfectly clear and obvious to me, but then I'm an expert in such
    things - I can't objectively assess how obvious it might be to less
    experienced programmers.
    James Kuyper, Jan 2, 2014
  4. *OH* *MY* *GOD*!! WHAT THE ****! Jesus merciful Christ! Wow! Talk about
    the suprise factor. *Completely* sailed over my head. Really cool. Were
    it not as beautiful as it is, I would call it an eldritch abomination.

    But you know what is the funniest thing in all this?

    I am actually well informed of the "assigment return value" (to use a bit
    LISPy terminology), and have, infact, used it without thinking twice
    about it, like in this function call...

    fill_32field((recursion ? (listento = get_nbnsaddr(scope)) :
    (listento = 0, brdcst_addr)),
    (unsigned char *)&(addr.sin_addr.s_addr));'s just that that `while' up there confused me. I have developed a
    reflex to never use a naked assigment operator in tests, in order to
    avoid the notorious "assignment when you meant test equality" bug, and
    that reflex kicked in when I saw the code snippet.
    Aleksandar Kuktin, Jan 2, 2014
  5. Or you can use the non-standard strlcpy() if you happen to have it (and
    your coding standards permit it). It returns the total length of the
    string it tried to create, as a size_t; you can add that to the base
    address to get a pointer to the end of the string.
    Keith Thompson, Jan 2, 2014
  6. (snip)
    With the tendency of compiler writers to add more and more
    warnings, justified or not, I don't believe in the requirement
    to code without warnings.
    There was a story many years ago about a school that had a
    compiler that would refuse to compile programs with obscene words
    in them, I believe even in comments. The students then figured out
    how to write them vertically, such that the compiler didn't notice.

    Also, another story about obscene words added in lower case, on
    a system where the printer ignored lower case characters. He was
    caught after a new printer was installed.

    -- glen
    glen herrmannsfeldt, Jan 2, 2014
  7. Matt

    Joe keane Guest

    How about this:

    const char foo[6] = "abcdef";
    Joe keane, Jan 2, 2014
  8. Perfectly legal to me, but I suppose some compilers will warn
    about it. If you want the null terminator, you should:

    const char foo[] = "abcdef";

    instead. Or:

    char *foo="abcdef";

    (with or without const) which has a different meaning, but
    sometimes is more useful.

    -- glen
    glen herrmannsfeldt, Jan 2, 2014
  9. What about it? What are you asking? (gcc 4.7.2 at least doesn't warn
    about it with "-Wall -Wextra".)
    Keith Thompson, Jan 2, 2014
  10. wrote:

    PL/I multiple assignment is of the form:

    A, B, C, D = 3;

    Where I believe that the right hand side is assigned to all
    the variables. Otherwise, = is both the relational operator
    and also used for assignment statments (it is not an assignment


    will assign 1 or 0 (converted from '1'B or '0'B) to A,
    depending on B equalling C, or not.

    -- glen
    glen herrmannsfeldt, Jan 3, 2014
  11. (snip, I wrote)
    In the case of an expression more complicated than 3, I suppose so.
    Well, someone coding


    probably should also get a wrist slapping.

    In Java, it would be considered a narrowing conversion and
    require a cast. That would also catch some of the others
    mentioned in this thread.

    -- glen
    glen herrmannsfeldt, Jan 3, 2014
  12. Matt

    Rosario193 Guest

    what about: "for(;*d=*s; ++d, ++s);"?
    Rosario193, Jan 3, 2014
  13. Matt

    Rosario193 Guest

    a^=a |i==0#.e|j==0#.e
    ..1: al=*i|*j=al|a==0#.2|j+=1|jc .e|i+=1|jc .e|#.1
    Rosario193, Jan 3, 2014
  14. Matt

    BartC Guest

    The above would be more like:


    in C. And in all these cases, where A, B, C, D are different types, they can
    all end up with different values anyway!

    Also in: A=B=C=A, it would be quite possible for A to change (using
    like your dbl:=intgr example).

    I think only with dynamic typing, would A, B, C, D all be assigned exactly
    the same value (in some languages, it's not even four separate copies of it,
    they would all refer to the same actual data!).
    BartC, Jan 3, 2014
  15. It's different to the original, but my first reaction is why write this
    as a for loop? There seems to be a tradition of "for abuse" in some

    while (*d = *s) ++d, ++s;

    Equally, this could be called "comma abuse" so maybe, if you want the
    alternate semantics, you might write it:

    while (*d = *s) { ++d; ++s; }
    Ben Bacarisse, Jan 3, 2014
  16. Matt

    Tonton Th Guest

    A sharp message from Theo Deraadt about strlcxx :
    Tonton Th, Jan 3, 2014
  17. No NULL terminator. String six chars long is stored in a buffer six chars
    Aleksandar Kuktin, Jan 3, 2014
  18. Matt

    Rosario193 Guest

    the only difference i see it is for "for", d and s point to a \0 byte
    at end

    and in the while loop d and s poiunt at end one char afther \0 one
    if someone want to use only "for loop" and never "while() loop", can
    simplify a little the language [one word less in the language]
    Rosario193, Jan 3, 2014
  19. Matt

    James Kuyper Guest

    True - which might or might not be a problem, depending upon whether or
    not the author intended that to be the case. Keith was probably well
    aware of that - the question is, what was Joe's point in bringing that up?
    James Kuyper, Jan 3, 2014
  20. Matt

    James Kuyper Guest

    Odd - I've used that construct occasionally, and I always wrote it the
    first way, which seems more natural to me. My compiler doesn't warn
    about that issue with the settings I use.
    How do you decide when to stop adding !=0? All of the following mean the
    same thing:

    while (a)
    while (a!=0)
    while ((a!=0) !=0)

    My policy is to stop adding !=0 before the first time.
    James Kuyper, Jan 3, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.