Pointer &&

Pages: 12
CODE1
1
2
3
4
if (pointer1 && pointer2)
{
//Do somthing if both pointers are not NULL
}


CODE2
1
2
3
4
if (pointer1 != NULL && pointer2 != NULL)
{
//Do somthing if both pointers are not NULL
}


is CODE1 == CODE2 ? Please explain..
Thanks
Last edited on
closed account (3hM2Nwbp)
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.
Last edited on
closed account (yUq2Nwbp)
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??
Last edited on
 
if(ss2) //<-- Still a bit ambiguous without seeing the variable declaration, but operator!  

i don't understand...

btw, i'd rather say the code isn't the same. what if one of the pointer is assigned as NULL?
closed account (yUq2Nwbp)
if one of the pointers is NULL then

if( pointer1 != NULL && pointer2 != NULL) condition is false
Last edited on
what i'm try to say is, what if:
1
2
3
4
5
6
7
8
9
10
if(pointer1 && pointer2) //both are NULL
{

}

//the purpose is obvious...
if(pointer1 != NULL && pointer2 != NULL)
{

}


the code isn't the same right? and if the code is considered same, isn't it your program will have a bug? from my opinion, yes
Last edited on
closed account (yUq2Nwbp)
these two if-s are the same......they verify the same condition.......can you tell me what means bug??
Last edited on
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...
closed account (yUq2Nwbp)
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??

but the pointer verifies with NULL or 0

what do you mean by this?

that's why i asked, what if the assignment is:
1
2
int * pointer;
pointer = NULL;


so:

 
if (pointer)


is the same with:

 
if (pointer != NULL)


or is it different? :)
Last edited on
closed account (z05DSL3A)
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.
what type it would be converted?
closed account (z05DSL3A)
Well, pointer, in your example, would be of type pointer to an int and it would be converted to a bool.
closed account (yUq2Nwbp)
Grey Wolf but can you answer how comparison is made??.....NULL as i know is the same 0 ...its implementation is

#define NULL 0

.....in both cases pointer must convert to bool........so processes are the same...
closed account (z05DSL3A)
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.
closed account (yUq2Nwbp)
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...
@grey wolf: if the result is return as bool (in case pointer is NULL) is it true?

@david: it is actually not "converted" but it is treated like the pointer is pointed to a variable with a NULL value. @grey: CMIIW
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).
closed account (yUq2Nwbp)
chipp if pointer is NULL, then it returns false.
@chipp wrote:
it is treated like the pointer is pointed to a variable with a NULL value.


Eh, what!? No, the pointer is NULL, it doesn't point to a NULL object or a variable whose value is NULL...
Last edited on
Pages: 12