为什么总是得到相同的布尔值
Why always get the same boolean value?
#include<stdio.h>
#include<stdlib.h>
int main ( void )
{
bool* Str_A = (bool*)calloc(1024,1);
bool* Str_B = (bool*)calloc(1024,1);
*Str_A = true;
*Str_B = true;
*(Str_A+2) = *Str_A+*Str_B;
printf("%s", (Str_A+2)?"true":"false");
}
无论我是否将*Str_A
或/和*Str_B
更改为false
,它总是打印true
.为什么?
这一行:
printf("%s", (Str_A+2)?"true":"false");
如果 Str_A
加 2 的地址不为零,则true
打印。您要检查的是 Str_A
加 2 的内容是否不为零,因此您应该编写:
printf("%s", *(Str_A+2)?"true":"false");
或者我更喜欢:
printf("%s", Str_A[2]?"true":"false");
在表达式 (Str_A+2)?"true":"false"
中,Str_A+2
是一个指针,因为它不是空指针,所以它被转换为 true
。
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 为什么布尔值不能比作最后一点?
- 为什么这个程序不输出布尔值?
- 为什么 SFINAE 没有给出增加布尔值的正确结果?
- 为什么我的变体将 std::string 转换为布尔值
- 为什么使用 int 的转换,而不是布尔值
- 为什么 lambda 会转换为值为真的布尔值?
- 为什么在布尔值上使用两个"!"运算符?
- 为什么提升circular_buffer存储我的布尔值?
- 为什么 std::atomic<bool> 比挥发性布尔值慢得多?
- 重载运算符<<输出布尔值.为什么
- 赋值操作在C中隐式计算为什么布尔值
- 为什么C++设置迭代器不能转换为布尔值?
- 为什么 isnan、isinf 和 isfinite 返回 int 而不是布尔值
- 为什么 std::string 没有隐式转换为布尔值
- 为什么总是得到相同的布尔值
- 返回布尔值时,为什么要使用"!!
- 为什么这个布尔值在 C++ 而不是 Java 中编译
- 为什么我应该使用 1 位位字段而不是布尔值
- 为什么我的全局布尔值总是导致false