What should I use NULL or 0

Started by
20 comments, last by The Parrot 17 years, 11 months ago
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?
Advertisement
It really doesnt matter, just be consistant.
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.
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.
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.
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.
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.
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.
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.
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.
Eric Wright o0Programmer0o

This topic is closed to new replies.

Advertisement