Compiler warning - suggest parentheses around assignment used as truth value

前端 未结 3 1837
难免孤独
难免孤独 2020-11-29 03:41

When I try to compile the piece of code below, I get this warning:

warning: suggest parentheses around assignment used as truth value

Why does t

相关标签:
3条回答
  • 2020-11-29 04:28

    Be explicit - then the compiler won't warn that you perhaps made a mistake.

    while ( (list = list->next) != NULL )
    

    or

    while ( (list = list->next) )
    

    Some day you'll be glad the compiler told you, people do make that mistake ;)

    0 讨论(0)
  • 2020-11-29 04:28

    While that particular idiom is common, even more common is for people to use = when they mean ==. The convention when you really mean the = is to use an extra layer of parentheses:

    while ((list = list->next)) { // yes, it's an assignment
    
    0 讨论(0)
  • 2020-11-29 04:28

    It's just a 'safety' warning. It is a relatively common idiom, but also a relatively common error when you meant to have == in there. You can make the warning go away by adding another set of parentheses:

    while ((list = list->next))
    
    0 讨论(0)
提交回复
热议问题