Why Compilation Does Not Fail?

十年热恋 提交于 2020-11-30 02:00:06

问题


As expected, C/C++ compilation does fail with "warning: comparison between pointer and integer" for the program below:

#include <stdbool.h>
int main(void) { return (int*)42 == true; }

But, the compilation does not fail when the true literal is changed to false. Why?

  • Confirmed for: clang-1100.0.33.12, gcc 7.5.0
  • Unable to confirm for: g++ 7.5.0

回答1:


In C, the macro false is defined as:

#define false   0

So you're comparing a pointer against 0, which is a valid null pointer constant.



来源:https://stackoverflow.com/questions/64708613/why-compilation-does-not-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!