Pointer &&

Pages: 12
Jun 14, 2011 at 5:37am
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 Jun 14, 2011 at 5:38am
Jun 14, 2011 at 5:45am
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 Jun 14, 2011 at 5:55am
Jun 14, 2011 at 7:54am
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 Jun 14, 2011 at 7:56am
Jun 14, 2011 at 8:26am
 
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?
Jun 14, 2011 at 8:31am
closed account (yUq2Nwbp)
if one of the pointers is NULL then

if( pointer1 != NULL && pointer2 != NULL) condition is false
Last edited on Jun 14, 2011 at 8:32am
Jun 14, 2011 at 9:10am
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 Jun 14, 2011 at 9:11am
Jun 14, 2011 at 9:53am
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 Jun 14, 2011 at 9:53am
Jun 14, 2011 at 12:30pm
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...
Jun 14, 2011 at 1:28pm
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??
Jun 14, 2011 at 2:14pm

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 Jun 14, 2011 at 2:15pm
Jun 14, 2011 at 2:41pm
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.
Jun 14, 2011 at 3:06pm
what type it would be converted?
Jun 14, 2011 at 3:34pm
closed account (z05DSL3A)
Well, pointer, in your example, would be of type pointer to an int and it would be converted to a bool.
Jun 14, 2011 at 4:39pm
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...
Jun 14, 2011 at 6:25pm
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.
Jun 14, 2011 at 6:35pm
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...
Jun 15, 2011 at 2:52am
@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
Jun 15, 2011 at 3:59am
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).
Jun 15, 2011 at 5:04am
closed account (yUq2Nwbp)
chipp if pointer is NULL, then it returns false.
Jun 15, 2011 at 5:24am
@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 Jun 15, 2011 at 5:25am
Pages: 12