Alternatives to modifying loop var in the loop.

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

  1. Apologies - I'm too trigger happy for my own good.

    I thought this is part of the str* discussion in the other part of the
    thread. And, in that context, I assumed Joe was asking bringing up
    something along the lines strlcpy vs. non-terminated string but, again, I
    confused the discussion in which this series of posts appears.
    Aleksandar Kuktin, Jan 3, 2014
    1. Advertisements

  2. Yes, I know. BTW, NULL is (a macro that expands to) a null *pointer*
    constant; it shouldn't be used to refer to the null character.
    Keith Thompson, Jan 3, 2014
    1. Advertisements

  3. Sorry for the noise. Confused the discussion.
    Aleksandar Kuktin, Jan 3, 2014
  4. My policy (not to imply that there's anything wrong with yours)
    is to use an explicit comparison when the value being tested is not
    "boolean", i.e., when it carries information beyond "zero is false,
    non-zero is true". For example, I don't use a pointer value directly
    as a condition; I'd write "if (ptr != NULL)" rather than "if (ptr)".
    Keith Thompson, Jan 3, 2014
  5. He stops there, because that form (apparently) silences a warning. The
    explanation was in the immediately following text that got snipped: "If
    I have to change ... to ... /to avoid warnings that's fine with me/.".
    Ben Bacarisse, Jan 3, 2014
  6. Matt

    James Kuyper Guest

    Yes, but he also explaining that he was willing to turn off warnings
    that he considered unjustified - implying that he considers this one
    justified. I'm don't remember if my compiler has the option to warn
    about such things, but if it did, I've turned it off. So my question was
    equivalent to asking "why don't your turn that warning off?".
    James Kuyper, Jan 3, 2014
  7. No, not a string but six characters are stored there.

    It is short for:

    const char foo[6]={'a', 'b', 'c', 'd', 'e', 'f',};

    which has many uses. (Among others, note that sizeof gives
    the right size in both cases, but not if you add the null.)

    If you really want the string, leave off the 6, the compiler will
    count them for you, including the null. Compilers count better
    than people do.

    -- glen
    glen herrmannsfeldt, Jan 3, 2014
  8. wrote:

    The wording doesn't sound like warning wording. "Cannot"
    should be used for violations of the standard language.
    Maybe "should not" or "preferably not" would be better.

    I usually put at least one space. I suppose some might use {}.

    Since the above loop has no other effect than to set i equal to n,
    unless n is negative, I wouldn't be against the warning.

    If the loop has more useful side effects, though, I would rather
    not have a warning.

    I don't remember ever having made that mistake.
    It is always cost vs. benefit. Reading warnings takes time,
    and so has a cost. The cost, multiplied by its probability
    should be balanced against the benefit, that is, the cost
    and probability of missing the bug.

    -- glen
    glen herrmannsfeldt, Jan 4, 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.