Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

vbisme

pointer = 0 same as pointer = null?

This topic is 5979 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Sometimes. It all depends on which library sets it. For most console apps, that would be the iostream library, which defines NULL as 0, so the answer in that case is yes. I''m not certain of the ANSI standard definition, but I think it''s something like a void pointer to 0. It''s all the same, in effect.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

Share this post


Link to post
Share on other sites
The windows.h defines NULL as 0. NULL is not part of the C / C++ standard, so I prefer to say my pointers = 0. Just a matter of style.

Share this post


Link to post
Share on other sites
well on most systems NULL would be 0 since that (and 0xFFFFFFFF on a 32bit system) are the two best values to use since things should not be allocated that low (reserved for system stuff like the vector interupt table, etc.). i ussually use NULL, but just to give myself an easier time reading code (especially functions that accept pointers).

Share this post


Link to post
Share on other sites
This brings me to a question. Often in C++ code we see a line like:

if (!MyPointer)
{
...
}

now this would assume that NULL==0 so my question is is this a safe practice? or is it preferred if we do:

if (MyPointer == NULL)

Thanks

Share this post


Link to post
Share on other sites
I do the first one (saves keystrokes). I''m not aware of any portability issues with it either.


Mike

"The state is that great fiction by which everyone tries to live at the expense of everyone else." - Frederick Bastiat, The Law

Share this post


Link to post
Share on other sites
Poya, C/C++ compilers only allow pointers to be tested against zero if you don't use typecasting. So if( !Pointer ) is always safe and will always work, assuming you set your pointers to zero (or NULL or whatever) to indicate they're invalid.

Technically, pointers can be any value. It all depends on how you want to treat it. You can set pointers to 0xFFFFFFFF to indicate they're invalid if you wish, if you use typecasting. if( (UINT)Pointer != 0xFFFFFFFF) though there's no real reason to do it that way.

~CGameProgrammer( );



Edited by - CGameProgrammer on February 6, 2002 11:55:23 PM

Share this post


Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!