Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of NULL != value in C++? [duplicate]

Tags:

c++

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.

like image 775
Anand Avatar asked Jul 26 '11 12:07

Anand


People also ask

What does != null mean?

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".

Is null and \0 the same?

'\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.

Is null the same as 0 in C?

NULL is 0 . NULL should be used with pointers, not integers.

Is if () the same as if null?

There's no functional difference; they are equivalent and they both test for null pointer.


2 Answers

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.

like image 28
Luchian Grigore Avatar answered Oct 15 '22 09:10

Luchian Grigore


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.

like image 102
Armen Tsirunyan Avatar answered Oct 15 '22 10:10

Armen Tsirunyan