2

In C or C++, isn't NULL just the constant integer 0? Why are we allowed to assign some pointer to the value NULL if it a constant integer? Wouldn't a compiler say that the two types don't match?

3 Answers 3

4

The interpretation of the constant 0 depends on the context. If it's being used in a context where a number is required, it's the number zero. If it's being used in a context where a pointer is required, it's treated as a null pointer. So if you have the code:

int n = 0; // assigns zero int *p = 0; // assigns null pointer somefunc((char *)0); // passes null pointer to the function 

In C, the macro NULL is permitted to expand into either an integer constant 0 or such a constant cast to void *. In C++ pre-11, it can expand into either an integer constant that evaluates to 0. In C++-11 it can expand into an integer literal with value 0 or a prvalue of type std::nullptr_t (e.g. nullptr).

See http://en.cppreference.com/w/cpp/types/NULL for the C++ description, http://en.cppreference.com/w/c/types/NULL for the C description.

Sign up to request clarification or add additional context in comments.

9 Comments

In C++, it's allowed to expand to any null pointer constant, including nullptr.
@BaummitAugen but in C++ it's not allowed to be (void*)0 since C++ doesn't do implicit conversion of void* like C does.
The question was about the definition of NULL.
@MarkRansom Sure, wasn't disputing that.
en.cppreference.com/w/cpp/types/NULL says it must be an integer literal or a value of type std::nullptr_t. Is (void*)0 one of those?
|
2

There are two issues:

  1. A type can have a "special", out-of-band, exceptional value. For example, floating-point numbers have the value NaN. And pointers have a special, not-a-valid-pointer value, the null pointer.

  2. What's the name of the null pointer? In C, for better or worse, its name is 0. (It would avoid all kinds of confusion if its name were a separate keyword like null or nil, but that's not the way the language definition came out.)

But yes, you're right, the ability to assign the constant 0 to a pointer variable is a special case, enshrined in the language definition.

3 Comments

So then is nullptr literally just memory location "0"? And also, my professor said that for functions that return a pointer to an element in an array, more and more people are assigning the pointer to the element after the last one in an array if some condition is not met, rather than assigning the pointer to NULL or nullptr. Is that actually true?
@HowardWang It is not necessarily memory location 0. But for every followon question you can imagine, see the C FAQ list.
@HowardWang modern C++ uses iterators that are similar in concept to pointers. All the standard library functions take two iterators to define a range, with the second one pointing to one-after the last valid element. Returning a "not found" value from these functions means returning the end iterator. That's probably exerting some influence on how pointers are used.
1
6.3.2.3 Pointers
...
3     An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
66) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.19.

C 2011 online draft

A literal 0 in a pointer context is treated specially by the compiler, and is understood to be the null pointer constant. In the context of your source code, it behaves like any other zero-valued expression.

However, once your code is translated to machine code, all occurrences of the null pointer constant will be replaced with whatever value the underlying platform uses to represent a well-defined invalid pointer value, whether that’s 0 or 0xFFFFFFFF or 0xDEADBEEF.

2 Comments

While the standard allows the possibility of null pointers that aren't physically all zero-bits, I'm not aware of any architectures that work that way. It's just easier and more efficient to use the literal bit pattern of zero.
@MarkRansom -- I have heard tales of ancient systems with null pointers which were not all bits zero.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.