Jump to content
  • Advertisement
Sign in to follow this  
FlyingDemon

Question about NULL

This topic is 4786 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
No:

#define NULL 0
char* tempPtr = NULL;

The line: char* tempPtr = NULL; is then char* tempPtr = 0; after the preprocessor substitues in 0 for NULL. The point of the define is to remap one symbol/expression into something else. In this case, NULL and 0 are the same thing.

Share this post


Link to post
Share on other sites
Quote:
Original post by FlyingDemon
If NULL is defined as 0 would it be wrong to set pointers to 0 instead of NULL?


It isn't necessarily *wrong* it's just the same thing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
No:

#define NULL 0
char* tempPtr = NULL;

The line: char* tempPtr = NULL; is then char* tempPtr = 0; after the preprocessor substitues in 0 for NULL. The point of the define is to remap one symbol/expression into something else. In this case, NULL and 0 are the same thing.


Actually in C, NULL is defined as :


#define NULL (void*)0



In c++ it's basically 0.

Share this post


Link to post
Share on other sites
NULL is just absence of a value. I know that in Java, NULL is actually infact, absent of a value. In C/C++ you just have to settle with 0 really, although if the implementation changed (for whatever daft reason), you could just redefine NULL to be the real implementation of NULL.

Share this post


Link to post
Share on other sites
Ok. I was just woundering because everytime I needed to use NULL I had to include a header that defined it like stdlib or define it myself.

Share this post


Link to post
Share on other sites
Quote:
Original post by FlyingDemon
Ok. I was just woundering because everytime I needed to use NULL I had to include a header that defined it like stdlib or define it myself.


Well NULL is also just a convention to use, so don't include more header files just to get that. Are you using NULL for any reason in particular?

Share this post


Link to post
Share on other sites
Quote:
Original post by FlyingDemon
Ok. I was just woundering because everytime I needed to use NULL I had to include a header that defined it like stdlib or define it myself.
stddef.h is the standard header if you only need NULL (or size_t/ptrdiff_t/offsetof).

Additionally GCC checks for use of NULL in non-pointers contexts so it basically acts the same way as a (void *) 0 definition would in C, but with warnings instead of errors as the result of any abuse.

It's mostly a matter of style/taste however. So if you're uncomfortable with having to include it or just think 0 looks better then feel free to switch.
Personally I prefer to use NULL.

Share this post


Link to post
Share on other sites
I still prefer Scott Meyer's* Templatized-Null-On-Crack™.

Enigma

*It may well not be his invention, but it was his book which introduced me to the concept.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!