c – Compiler warning – suggest parentheses around assignment used as truth value

c – Compiler warning – suggest parentheses around assignment used as truth value

Be explicit – then the compiler wont warn that you perhaps made a mistake.

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

or

while ( (list = list->next) )

Some day youll be glad the compiler told you, people do make that mistake 😉

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, its an assignment

c – Compiler warning – suggest parentheses around assignment used as truth value

Its 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))

Leave a Reply

Your email address will not be published.