At least in Microsoft's compilers NULL is just a macro for (void*)0 or 0 depending on your compiler settings. <- Edited
1 2 3 4 5 6 7 8 9
if(pointer1 && pointer2)
{
// if pointer1 is not 0 and pointer2 is not 0
}
if(pointer1 != NULL && pointer2 != NULL)
{
// if pointer1 is not 0 and pointer2 is not 0
}
In short, yes, it's the same. Personally I prefer the second style as it's more verbose, especially when dealing with objects that have operator! or operator== overloaded (like std::stringstream).
1 2 3 4 5 6 7 8 9
std::stringstream* ss1;
std::stringstream ss2;
if(ss1) //<-- Pointer, or non-pointer?
if(ss2) //<-- Pointer, or non-pointer?
1 2 3 4 5 6 7 8
std::stringstream* ss1;
std::stringstream ss2;
if(ss1 != NULL) //<-- Ah, pointer
if(ss2) //<-- Still a bit ambiguous without seeing the variable declaration, but operator!
* C++0x is adding the nullptr keyword to put the last coffin nail in the '0 vs NULL' war.
rajimrt there are the same codes.....explain why...your conditions work with true and false.....
your pointer1!=NULL returns true or false...........if you write this
if(pointer)
the compiler will verify if pointer==0 or not..........if your pointer isn't 0 it doesn't matter what it is, it returns true........in other words if 0 then false and if 1 or 100 or 145287 or -14 then it returns true...ok??
bug, as far as i know, error of your program, oftenly refer to logic error...
ok, maybe i was wrong, what is the reason you're saying that the to ifs are the same? btw, if the first condition (the pointer is both NULL) is the if statement will evaluate true? or false? if that so, maybe, both if is the same...
chipp can you repeat your question one more time??....i didn't understand it very well.......well lets suppose we have these two following codes
if (pointer) and if (pointer!=NULL)
......these two codes are the same.........in the second condition i think it is clear........important thing is that != operation returns true or false.........in the first condition verifying is not obvious but the pointer verifies with NULL or 0.........in other words when you write if(pointer), compiler understand if(pointer != NULL).............ok??
Is if(pointer) the same as if(pointer != NULL)?
The outcome would be the same but the method are different. The first would use implicit type conversion and the second would do a comparison.
no, the null pointer constant is converted to an appropriate pointer to object with a null pointer value. The value of the pointer is compared to this and the result is returned as a bool.
well...........if what you said is correct then can you tell me how NULL which defined 0, can convert to object*??......correct me if i'm mistaken......i only want to understand what you say...
if (ptr1 && ptr2) is far better style and is in accordance with the semantics for smart pointers (which you should be using in most cases, by the way).