Go Down

Topic: Can a pointer be null and if so can it be tested for? (Read 1 time) previous topic - next topic


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.


Nick Gammon

I believe that conformant compilers are supposed to accept:

Code: [Select]

if (ptr)


... as a valid test for a non-null pointer, regardless of the internal representation of pointers.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Dyslexicbloke

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

Coding Badly

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.

Dyslexicbloke

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

Thanks again folks.
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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy