问题
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