Possible Duplicate:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)?
I was going through a piece of C++ code and came across a code like
if (NULL != threadInfo)
{
...
...
}
I was just wondering is there any difference between using the expression
if (threadInfo != NULL)
{
...
...
}
what is said above. While reading the first one reads " If NULL not equals to ThreadInfo" and the second one reads "If threadInfo not equals to NULL". To me the second one makes more sense.
NULL is not a value. It is literally the absence of a value. You can't "equal" NULL! The operator != does not mean "is not"; it means "is not equal to".
'\0' is defined to be a null character. It is a character with all bits set to zero. This has nothing to do with pointers. '\0' is (like all character literals) an integer constant with the value zero.
NULL is 0 . NULL should be used with pointers, not integers.
There's no functional difference; they are equivalent and they both test for null pointer.
It's for safety, so you don't accidentally write
threadInfo = NULL
instead of
threadInfo == NULL
For the != there's no need to do this, but it's consistent.
No, there is no difference. In case of ==
there might be some difference. The thing is that if you accidentally write =
instead of ==
the compiler will give an error in the first case.
if (threadInfo = NULL) //always false. The compiler will give you a warning at best
if (NULL = threadInfo) //Compiler error
I personally hate that practice and think it's better to write code that can be read in a normal human language, not Yoda language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With