NULL for databases, NIL for code.
I like the pre-defined NULL macro, as it preserves the semantic meaning, rather than some other use of the number 0.
There are many English words which are spelled or spoken alike, yet which have different meanings. Like in English, use the context in which the discussion is taking place to guide you toward the intended meaning.
I use '\0'
for the nul-character and NULL
for pointers because it is clearest in both cases.
BTW, both 0
and '\0'
are int
s in C and either one will be converted to char
when stored in a char
variable.
@BKB:
I see the point in his advice, but "NULL" makes it clearer that the context is pointers. It's like using "0.0" for floating-point values, as '\0' when dealing with characters. (Likewise, I prefer seeing 0 if a char is being used in an arithmetic context.)
Bjarne further states in this FAQ that NULL is #defined as 0 anyway, so standard code shouldn't have a problem with it. I agree that the all-caps notation is ugly, but we'll have to wait until 0x (where nullptr will be available, as a keyword.)
For communication I use NULL. If I'm working with a developer who cannot grasp the concept of NULL for different data-types then I'd be concerned.
For implementation it's case-specific. Numbers are 0 (post-fixed f for floating-point), pointers are NULL and character strings are 0.
Systems that don't use binary 0 for NULL are getting harder to find. They also tend to have various portability issues. Why? Because on these systems neither memset nor calloc can clear out a struct that contains pointers correctly.
const char END_OF_STRING = '\0';
So when you say:
str[i] = END_OF_STRING;
or
if (*ptr == END_OF_STRING)
there is absolutely no question what you mean.
I quite like
#define ASCII_NUL ('\0')
I only very occasionally mistype '\0' as '0'. But when I have done it, I've found the error very hard to spot by code inspection, with hilarious consequences. So I don't like '\0' much, and prefer ASCII_NUL or 0 (of course the latter has the wrong type in C++). Obviously I use '\0' where demanded by consistency with existing code, or style guides.
The Google C++ style guide, which contains a few things I like and a few I don't, but seems mostly sound, prefers NULL to 0 for pointers. It points out that NULL might not be defined simply as 0 (or 0L), especially in implementations where sizeof(void*) might not be sizeof(int) (or sizeof(long int)).
0 and NULL are both specified to be of integral type, and when converted to a pointer type they both must yield a null pointer value. But they aren't necessarily of the same integral type. So you might conceivably get some useful warnings or errors in some situations by using NULL.
We use NULL for pointers and NULLCHAR for characters, using
#define NULLCHAR '\0'
While, on the whole, I would advice using named constants, this is one exception. To me, defining:
#define NULL 0
#define END_OF_STRING '\0'
makes as much sense as defining:
#define SEVEN 7
which is none. And yes, I am aware that NULL is already defined by the compiler, but I never use it. For pointers, 0; for chars, '\0'. Longer does not always mean more expressive.
If I remember correctly most C compilers define NULL like this:
#define NULL ((void*)0)
This is to ensure that NULL is interpreted as being a pointer type (in C). However this can cause issues in the much more type strict world of C++. Eg:
// Example taken from wikibooks.org
std::string * str = NULL; // Can't automatically cast void * to std::string *
void (C::*pmf) () = &C::func;
if (pmf == NULL) {} // Can't automatically cast from void * to pointer to member function.
Therefore in the current C++ standard null pointers should be initialized with the literal 0. Obviously because people are so used to using the NULL define I think a lot of C++ compilers either silently ignore the issue or redefine NULL to be 0 in C++ code. Eg:
#ifdef __cplusplus
#define NULL (0)
#else
#define NULL ((void*)0)
#endif
The C++x0 standard now defines a nullptr
keyword to represent null pointers. Visual C++ 2005's CLI/C++ compiler also uses this keyword when setting managed pointers to null. In current compilers you can create a template to emulate this new keyword.
There is a much more detailed article on wikibooks.org discussing this issue.
A one-L NUL, it ends a string.
A two-L NULL points to no thing.
And I will bet a golden bull
That there is no three-L NULLL.
(The name of the original author is, alas, lost to the sands of time.)
Sort of related: Slashdot recently had a story on the comp.lang.c
FAQ section on null pointers, which I found quite interesting.