Pages: 1 [2]   Go Down
Author Topic: Can a pointer be null and if so can it be tested for?  (Read 1086 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Old habit.
No problem. Usually, I prefer explicit comparisons (myPtr != NULL), except in the case of pointer (myPtr). I'm not sure why.

I'm in the habit of explicit comparisons also. Part is computer science purism (NULL is a concept, not a particular value), part is influence from other languages, part is one particular project.

if(myPtr) is not a problem when NULL is defined as zero, which is true 99.99...% of the time.

I have worked on software where NULL was not equal to zero. Granted, it's extremely rare and a little bizarre, but it did reinforce the habit of explicit comparisons.

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe that conformant compilers are supposed to accept:

Code:
if (ptr)

... as a valid test for a non-null pointer, regardless of the internal representation of pointers.
Logged

Staffordshire UK
Offline Offline
Full Member
***
Karma: 1
Posts: 132
Just a little bit out there ...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am following all the reasoning and I agree testing for a positive condition id the option but this business of syntax ..
If(MyPointer) OR If(MyPointer != NULL)

I assume that they are functionally the same once compiled, and I want to be consistent when coding ....
Any chance we could have a consensus,
I haven't a clue so will go with the short version given no other selection criteria.

Thanks
folks
Logged

If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 198
Posts: 12747
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I assume that they are functionally the same once compiled

Yes.

Quote
Any chance we could have a consensus,

PaulS and Nick Gammon both stated the "!= NULL" is not required.  I call that a consensus.
Logged

Staffordshire UK
Offline Offline
Full Member
***
Karma: 1
Posts: 132
Just a little bit out there ...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's 3 votes then ... good enough for me ' If(MyPointer) ' it is

Thanks again folks.
Logged

If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

Pages: 1 [2]   Go Up
Jump to: