Jump to content
  • Advertisement
Sign in to follow this  
Japanese

What should I use NULL or 0

This topic is 4418 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

I know NULL has been has defined to 0. But some people say it's better if you use 0. We should not rely on NULL. Can anyone explain this?

Share this post


Link to post
Share on other sites
Advertisement
It's not 'better' in any way shape or form. Some people use null to be explicit and more obvious when setting pointers to 0. NULL is just #define NULL 0 anyways.

Share this post


Link to post
Share on other sites
Personally I use 0 because I feel it looks cleaner and better shows my intent. There can be some minor problems with NULL though, some compilers define it differently (0UL, (void*)0, 0 etc.) which might give you some problems on non-standard compilers. Apart from that I can't see any real problems NULL could give you.

Of course there is also the problem of clearly showing what you mean. Null is actually a value which should be used to represent a null-pointer. A null-pointer is a pointer which explicitly show that it doesn't have a target (not necessarily by being 0). So using NULL for anything but pointers might confuse some people.

Share this post


Link to post
Share on other sites
Quote:
Original post by DrEvil
NULL is just #define NULL 0 anyways.


This is the kind of stuff which can lead to confusion, the standard states:
Quote:
The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (4.10). 180)

180) Possible definitions include 0 and 0L, but not (void*)0.

Share this post


Link to post
Share on other sites
So, what I interpret is-->
I can use 0 anywhere and if I have to use NULL then I should limit it to pointers only. Is that right.

Share this post


Link to post
Share on other sites
Yes you should definately only use NULL for pointers; not ints or anything. If a compiler gives you a problem with NULL, you can just redefine it to 0 anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Japanese
So, what I interpret is-->
I can use 0 anywhere and if I have to use NULL then I should limit it to pointers only. Is that right.


What is promised is that that every pointer can be assigned to NULL, and NULL is zero (not necessarily 0, could be 0U, 0L etc.). So you're free to assign any pointer to NULL. NULL is an integral constant expression so you can also assign an integral (char, signed char, short int, int, long int and their unsigned variants) to NULL. In C++ (and C) NULL is guarenteed to be zero so you can depend on that behavior, but programmers from other backgrounds might be a little confused when you use NULL instead of 0. In general I wouldn't use NULL at all, but this is just personal preference, like Nitage said, if you're consistant then it really doesn't matter.

Share this post


Link to post
Share on other sites
I also think it is a good idea to use NULL for pointers.
A NULL shows me a variable holding a address, a 0 shows me a variable holding a number.

BTW 0 and NULL are realy the same, if there's a problem with your compiler just define a constant (like #define NULL 0) and you get along with your compiler <BG>.

MFG Damien K.

Share this post


Link to post
Share on other sites
If your going to school you may want to use NULL for pointers. My teacher marked a poitn off my test becasue I used a zero instead of NULL because its three less letters to write.

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!