c++ - 为什么编译不会失败?

标签 c++ c compiler-errors compiler-warnings

不出所料,C/C++编译的确会因以下程序的“警告:指针与整数之间的比较”而失败:

#include <stdbool.h>
int main(void) { return (int*)42 == true; }
但是,当true文字更改为false时,编译不会失败。为什么?
  • 确认用于:clang-1100.0.33.12,gcc 7.5.0
  • 无法确认:g++ 7.5.0
  • 最佳答案

    在C中,宏false定义为:

    #define false   0
    
    因此,您正在将指针与0(一个有效的空指针常量)进行比较。

    关于c++ - 为什么编译不会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64708769/

    相关文章:

    C - time_t 字符串表示

    c - HeapReAlloc 改变内存地址后是否应该使用HeapFree?

    c# - 为什么我不能在数组上调用 RemoveAt?

    c++ - 了解语法 : crc ^= *((unsigned char *)memptr)++;

    c++ - std::atoll 与 VC++

    c++ - 检查字符串中的唯一字符

    c++ - `x--> 0 && array[x]` 是 C++ 中定义明确的行为吗?

    c++ - 如何使用 xmlsec 和 CSP 对 XML 进行签名?

    c - 函数指针问题

    C++ 新对象