用C始终得到真正的 [英] Always getting true in C

查看:163
本文介绍了用C始终得到真正的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我简单的程序

char *array = 1==1;
if((bool)array == true)
printf("true");

Output: true

但是,当我改变阵列中的

But when I change the array in

*array = 1==2;
Output: true

我不知道为什么它总是如此。我只想打印如果变量阵列真正

推荐答案

如果你这样做:

char *array = 1==2;

然后(布尔)阵列 是假的,像你期望的那样,是因为你刚刚写设置指针的一个巨大的奇怪的方式'阵'为空(零)。

Then (bool)array WILL be false, like you expect, because you've just written an enormously strange way of setting the pointer 'array' to null (zero).

不过,如果相反,你做

char *array = 1==1;
*array = 1==2;

那么你所做的是初始化阵列是指向解决'1'指针,然后写了一个'假'(零)到该地址。这几乎肯定会崩溃,在任何现代平台的程序,虽然在一个小嵌入式平台其他的事情(大部分可能不期而至你)可能会发生。

Then what you've done is initialised array to be a pointer pointing to address '1' and then written a 'false' (zero) to that address. This will almost certainly crash your program on any modern platform, though on a small embedded platform other things (probably mostly unexpected to you) might happen.

这篇关于用C始终得到真正的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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