编译器警告 - 建议在赋值周围使用括号作为真值 [英] Compiler warning - suggest parentheses around assignment used as truth value

查看:32
本文介绍了编译器警告 - 建议在赋值周围使用括号作为真值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译下面的代码时,我收到了这个警告:

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

警告:建议使用括号括起来作为真值

为什么会这样?这是一个相当普遍的成语,我相信.我什至早先在我的代码中使用了类似的东西.

Why does this happen? This is a rather common idiom, I believe. I even use something like it earlier on my code.

struct PIDList* 
getRecordForPID(struct PIDList* list, pid_t pid) {
    while(list = list->next)
        if (list->pid == pid)
            return list;

    return NULL;
}

推荐答案

要明确 - 这样编译器就不会警告你可能犯了错误.

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

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

while ( (list = list->next) )

总有一天你会很高兴编译器告诉你,人们确实犯了这个错误;)

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

这篇关于编译器警告 - 建议在赋值周围使用括号作为真值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆