评估if表达式的行为不同于评估BOOL表达式 [英] Evaluating an if expression behaves differently then evaluating a BOOL expression

查看:182
本文介绍了评估if表达式的行为不同于评估BOOL表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设这两个 if 语句的结果始终是相同的

i assumed that the outcome of these two if statements is ALWAYS the same

BOOL tmp = (expression);
if (expression) {

}

if (tmp) {

}

但发现此处

像检查 0xF00 指针时,可能并不总是这样...

like when checking against a 0xF00 pointer, this might not be always the case...

c>

推荐答案

code> if 语句定义在 spec 。相关部分是:

The if statement is defined in section 6.8.4.1 of the spec. The relevant section is:


2在两种形式中,如果表达式不等于0,则执行第一个子语句。

2 In both forms, the first substatement is executed if the expression compares unequal to 0.

你会注意到在该语句中任何地方缺少 BOOL BOOL 在C中不存在;它只是Cocoa提供的 signed char 的typedef。 ( bool 存在于C的现代版本中,但它是一个不同的类型,并且仍然与 if 语句无关。 )

You will note the lack of BOOL anywhere in that statement. BOOL doesn't exist in C; it's just a typedef to signed char provided by Cocoa. (bool exists in modern versions of C, but it's a different type, and still not related to the if statement.)

所以如果 expression 比较不等于0,但 tmp 比较等于0,则它们的行为会不同。这是汤姆·哈灵顿在评论中提出的观点。

So if expression compares unequal to 0, but tmp compares equal to 0, then they will behave differently. This is the point Tom Harrington was making in the comments.

这篇关于评估if表达式的行为不同于评估BOOL表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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