Hacker News new | ask | show | jobs
by johnisgood 2297 days ago
In Modern C (book mentioned by OP), there is takeaway 2.11.1.14 which says "Don't use NULL" because:

The definition in the C standard of a possible expansion of the macro NULL is quite loose; it just has to be a null pointer constant. Therefore, a C compiler could choose any of the following for it: 0U, 0, '\0', 0UL, 0L, 0ULL, 0LL, or (void * )0. It is important that the type behind NULL is not prescribed by the C standard. Often, people use it to emphasize that they are talking about a pointer constant, which it simply isn’t on many platforms. Using NULL in a context that we have not mastered completely is even dangerous. This will in particular appear in the context of functions with a variable number of arguments.

NULL hides more than it clarifies. Either use 0 or, if you really want to emphasize that the value is a pointer, use the magic token sequence (void * )0 directly.

https://www.gnu.org/software/libc/manual/html_node/Null-Poin... says:

The preferred way to write a null pointer constant is with NULL. You can also use 0 or (void * )0 as a null pointer constant, but using NULL is cleaner because it makes the purpose of the constant more evident.

If you use the null pointer constant as a function argument, then for complete portability you should make sure that the function has a prototype declaration. Otherwise, if the target machine has two different pointer representations, the compiler won’t know which representation to use for that argument. You can avoid the problem by explicitly casting the constant to the proper pointer type, but we recommend instead adding a prototype for the function you are calling.

https://man.openbsd.org/style says:

NULL is the preferred null pointer constant. Use NULL instead of (type * )0 or (type * )NULL in all cases except for arguments to variadic functions where the compiler does not know the type.

---

Readers: what is your take on it and why?

3 comments

> Readers: what is your take on it and why?

It's ok to draw a line on what implementations you care about. And honestly, at this day and age, I couldn't care less about implementations that define NULL to be 0 (or something else that isn't a pointer). It's a case of where it's not worth everyone's time to be catering for the lowest common denominator.. instead, fix that implementation or just go download a better one.

If I could make breaking changes to C today, I would remove the NULL macro and replace with a lowercase null (or nil or whatever) keyword a definition that doesn't impose pointless burden on its users.

That said, I'm still very much in the habit of casting NULL to void* when I'm using it as an argument for variadic functions. It's silly, but that's just how we do things...

You could make a proposal to the C committee to add a keyword `_Nullptr` and to add a header `stdnull.h` which has `#define nullptr _Nullptr`, like they did with bool.
Yep. I'd be happier to work with the C committee if they were a little more open. For example, their mailing list could be public.
> Readers: what is your take on it and why?

All the bad stuff that people on both sides of this argument predict is going to happen somewhere in some code base, precisely because people on both sides can't agree and think the world would be better if everyone agreed on those people's personal aesthetic ideals.

So... don't sweat it. Conform to whatever code base you're working on has decided, and if it's inconsistent work to make it so.

And above all, no matter what else: Don't start dumb fights over this nonsense. Yeesh.

Use NULL, this is what it's for. 0 will convert the the NULL pointer representation, but the NULL pointer representation is not required to be bitwise identical to 0.