无意中使用=代替== [英] Inadvertent use of = instead of ==

查看:118
本文介绍了无意中使用=代替==的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎

if (x=y) { .... }

而不是

if (x==y) { ... }

是万恶之根。

为什么不的所有的编译器的将其标记为错误的,而不是一个可配置的警告?

Why don't all compilers mark it as error instead of a configurable warning?

我感兴趣的是找出情况下构建如果(X = Y)是很有用的。

I'm interested in finding out cases where the construct if (x=y) is useful.

推荐答案

在大多数情况下,编译器非常努力保持向后兼容。

Most of the time, compilers try very hard to remain backward compatible.

改变他们的行为在这个问题抛出的错误会破坏现有的合法code,甚至开始抛出警告,它会导致与自动编译它,并检查跟踪code自动系统的问题错误和警告。

Changing their behavior in this matter to throw errors will break existing legit code, and even starting to throw warnings about it will cause problems with automatic systems that keep track of code by automatically compiling it and checking for errors and warnings.

这是一个邪恶的我们pretty多坚持了自动取款机,但有办法规避,降低了它的危险性。

This is an evil we're pretty much stuck with atm, but there are ways to circumvent and reduce the dangers of it.

例如:

   void *ptr = calloc(1, sizeof(array));
   if (NULL = ptr) {
       // some error
   }

这会导致编译错误。

这篇关于无意中使用=代替==的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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