tags:

views:

731

answers:

15
+1  A: 

NULL for databases, NIL for code.

JosephStyons
+5  A: 

I like the pre-defined NULL macro, as it preserves the semantic meaning, rather than some other use of the number 0.

Matt J
+4  A: 

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.

Justice
+10  A: 

I use '\0' for the nul-character and NULL for pointers because it is clearest in both cases.

BTW, both 0 and '\0' are ints in C and either one will be converted to char when stored in a char variable.

Robert Gamble
humm.i was under the impression that a character in single quotes is a char in c.
EvilTeach
In C++ a character constant with a single character is type char, in C a character constant is type int.
Robert Gamble
0 is _not_ `int` in C. It's a special integer literal which doesn't have type as such, but can be used in either `int` or pointer context (much like C# `null`).
Pavel Minaev
@Pavel: 0 *is* an int in C which is a regular integer value like any other. When the literal 0 is converted to a pointer the result is a null pointer and because of that 0 or (void *)0 is called a null pointer constant but that does not change the fact that before converting it to a pointer 0 is an integer and after converting it to a pointer it is no longer an integer but a null pointer. You might want to review section 5 of the c-faq (http://c-faq.com/null/index.html).
Robert Gamble
+2  A: 

@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.)

aib
A: 

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.

Andrew Grant
A: 

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.

Joshua
i certainly have never seen a NULL that didn't equate to zero.
EvilTeach
A: 
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.

James Curran
+3  A: 
Tom Barta
And to increase clarity, I typically explicitly check against NULL when using an assignment statement as in an if-statement: if ((myPtr = GetData()) != NULL) { /* do work on myPtr */ }
Isak Savo
A: 

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.

Steve Jessop
A: 

We use NULL for pointers and NULLCHAR for characters, using

#define NULLCHAR '\0'
Graeme Perrow
+1  A: 

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.

Gorpik
The difference as I see it is that "7" is a symbol which always means "seven" (as a C token, at least). '\0' is of course used as a string terminator, but that isn't always and inherently what a 0 byte means, so it's not synonymous with end-of-string, any more than 200 is synonymous with success.
Steve Jessop
That said, I don't see anything wrong with using '\0' as a literal, just because it sometimes is used for other things as well. But I also don't think that using a name is pointless, since there is a semantic difference even though the value of end-of-string will of course be 0 in all C programs.
Steve Jessop
I guess I was thinking more C++ than C here. \0 is end-of-string only for null-terminated strings. So I think it is much clearer than END_OF_STRING, which might not actually be and end-of-string. And END_OF_NULL_TERMINATED_STRING is getting things too far.
Gorpik
+3  A: 

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.

orj
NULL and 0 are equivalent in C++. You can use NULL to initialize pointers without a problem in both languages. Any C++ library that defines NULL in the old C-style is nonconforming.
Dan Olson
+1  A: 
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.)
EvilTeach
I really enjoyed that poem :-)
Anthony Cuozzo
A: 

Sort of related: Slashdot recently had a story on the comp.lang.c FAQ section on null pointers, which I found quite interesting.

Martin Geisler