How is it a keyword and an instance of a type?

This isn't surprising. Both true and false are keywords and as literals they have a type ( bool ). nullptr is a pointer literal of type std::nullptr_t, and it's a prvalue (you cannot take the address of it using &).

  • 4.10 about pointer conversion says that a prvalue of type std::nullptr_t is a null pointer constant, and that an integral null pointer constant can be converted to std::nullptr_t. The opposite direction is not allowed. This allows overloading a function for both pointers and integers, and passing nullptr to select the pointer version. Passing NULL or 0 would confusingly select the int version.

  • A cast of nullptr_t to an integral type needs a reinterpret_cast, and has the same semantics as a cast of (void*)0 to an integral type (mapping implementation defined). A reinterpret_cast cannot convert nullptr_t to any pointer type. Rely on the implicit conversion if possible or use static_cast.

  • The Standard requires that sizeof(nullptr_t) be sizeof(void*).

Answer from Johannes Schaub - litb on Stack Overflow
🌐
Reddit
reddit.com › r/cpp_questions › what is the difference between null and nullptr when using them for something like a binary search tree? are those interchangeable?
r/cpp_questions on Reddit: What is the difference between NULL and nullptr when using them for something like a Binary Search Tree? Are those interchangeable?
June 29, 2021 -

As far as I know, NULL is just another name for 0 (edit 3: I used the NULL from <iostream>)for which nullptr is a pointer pointing 0. Some references I have used NULL, but others have nullptr. They are writing their trees in different ways so I am not sure if they are interchangeable, but I saw someone on an overflow post saying that versions of C++ do consider NULL == nullptr. But my past experiences are saying that int *some operator here* pointer is a mess. Can anyone clear this up for me? Thank you!

edit:sample for NULL:TreeNode *leftPtr = NULL;TreeNode *rightPtr = NULL;

sample for nullptr:TreeNode *leftPtr = nullptr;TreeNode *rightPtr = nullptr;// idk if these work, or if they only work if:// for example rootPtr->rightPtr = nullptr

// edit 2: running this, it prints "1" so it must be true,

#include <iostream>

using namespace std;

int main() {
    cout << (NULL == nullptr) << endl;
    return 0;
}

Top answer
1 of 5
9
As far as I know, NULL is just another name for 0 It is not. If you look here , you will see that NULL is defined in all of: It is also defined in all of: You'll also find NULL defined in a lot of 3rd party libraries. And in all instances it is IMPLEMENTATION DEFINED. It's not at all unreasonable to find #define NULL ((void*)(0)) or #define NULL ((char*)(0)) or any other such nonsense, both of which are both INCORRECT AND NOT THE SAME THING. Problems arose when, for a misplaced sense of brevity, K&R decided to reuse integer zero in a different context, where null doesn't mean the same thing. I guess this saved them some punch card space, but it introduced a lot of misunderstanding and conflation of what both null and pointers are, and we've been dealing with this confusion as a source of bugs and exploits ever since. Then Bjarne admits he made the same mistake when declaring pure virtual methods as equal to zero. There are many scenarios that can arise where you mean an integer and get a null pointer, or want a null pointer and get an integer. nullptr is type safe. Never use NULL, there is no scenario where it is necessary or preferred, not even for backward compatibility or when interfacing with a C library.
2 of 5
7
nullptr is the null pointer literal introduced with c++11. It has implicit conversion to any pointer type. It's a keyword in c++. NULL is an implementation-defined null pointer constant, defined in a number of different headers (and I guess pulled in indirectly through your implementation of the iostream header).
Old compilers and NULL Mar 29, 2022
r/C_Programming
4y ago
[C23] The addition of nullptr and nullptr_t is bad Oct 1, 2023
r/C_Programming
2y ago
Is p > nullptr or similar undefined behavior in C++? Feb 3, 2024
r/cpp_questions
2y ago
More results from reddit.com
Top answer
1 of 15
460

How is it a keyword and an instance of a type?

This isn't surprising. Both true and false are keywords and as literals they have a type ( bool ). nullptr is a pointer literal of type std::nullptr_t, and it's a prvalue (you cannot take the address of it using &).

  • 4.10 about pointer conversion says that a prvalue of type std::nullptr_t is a null pointer constant, and that an integral null pointer constant can be converted to std::nullptr_t. The opposite direction is not allowed. This allows overloading a function for both pointers and integers, and passing nullptr to select the pointer version. Passing NULL or 0 would confusingly select the int version.

  • A cast of nullptr_t to an integral type needs a reinterpret_cast, and has the same semantics as a cast of (void*)0 to an integral type (mapping implementation defined). A reinterpret_cast cannot convert nullptr_t to any pointer type. Rely on the implicit conversion if possible or use static_cast.

  • The Standard requires that sizeof(nullptr_t) be sizeof(void*).

2 of 15
141

Why nullptr in C++11? What is it? Why is NULL not sufficient?

C++ expert Alex Allain says it perfectly here (my emphasis added in bold):

...imagine you have the following two function declarations:

void func(int n); 
void func(char *s);
 
func( NULL ); // guess which function gets called?

Although it looks like the second function will be called--you are, after all, passing in what seems to be a pointer--it's really the first function that will be called! The trouble is that because NULL is 0, and 0 is an integer, the first version of func will be called instead. This is the kind of thing that, yes, doesn't happen all the time, but when it does happen, is extremely frustrating and confusing. If you didn't know the details of what is going on, it might well look like a compiler bug. A language feature that looks like a compiler bug is, well, not something you want.

Enter nullptr. In C++11, nullptr is a new keyword that can (and should!) be used to represent NULL pointers; in other words, wherever you were writing NULL before, you should use nullptr instead. It's no more clear to you, the programmer, (everyone knows what NULL means), but it's more explicit to the compiler, which will no longer see 0s everywhere being used to have special meaning when used as a pointer.

Allain ends his article with:

Regardless of all this--the rule of thumb for C++11 is simply to start using nullptr whenever you would have otherwise used NULL in the past.

(My words):

Lastly, don't forget that nullptr is an object--a class. It can be used anywhere NULL was used before, but if you need its type for some reason, it's type can be extracted with decltype(nullptr), or directly described as std::nullptr_t, which is simply a typedef of decltype(nullptr), as shown here:

Defined in header <cstddef>:

See:

  1. https://en.cppreference.com/w/cpp/types/nullptr_t
  2. and https://en.cppreference.com/w/cpp/header/cstddef
namespace std
{
typedef decltype(nullptr) nullptr_t; // (since C++11)
// OR (same thing, but using the C++ keyword `using` instead of the C and C++ 
// keyword `typedef`):
using nullptr_t = decltype(nullptr); // (since C++11)
} // namespace std

References:

  1. Cprogramming.com: Better types in C++11 - nullptr, enum classes (strongly typed enumerations) and cstdint
  2. https://en.cppreference.com/w/cpp/language/decltype
  3. https://en.cppreference.com/w/cpp/types/nullptr_t
  4. https://en.cppreference.com/w/cpp/header/cstddef
  5. https://en.cppreference.com/w/cpp/keyword/using
  6. https://en.cppreference.com/w/cpp/keyword/typedef
🌐
Cppreference
en.cppreference.com › w › c › language › nullptr.html
Predefined null pointer constant (since C23) - cppreference.com
The keyword nullptr denotes a predefined null pointer constant. It is a non-lvalue of type nullptr_t.
🌐
Reddit
reddit.com › r/c_programming › [deleted by user]
[deleted by user] : r/C_Programming
July 23, 2025 - In C, NULL is an implementation-defined value that evaluates to the null pointer constant, possibly cast to void*. nullptr is an explicit null pointer value. As long as you are comparing them to pointers (like the return of fopen), then they are interchangeable.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › understanding-nullptr-c
Understanding nullptr in C++ - GeeksforGeeks
Unlike NULL, nullptr cannot be assigned to integer types. ... #include <iostream> using namespace std; int main() { int* ptr = nullptr; if (ptr) cout << "true"; else cout << "false"; }
Published: June 9, 2026
🌐
Embedded Artistry
embeddedartistry.com › home › blog › migrating from c to c++: null vs nullptr
Migrating from C to C++: NULL vs nullptr - Embedded Artistry
December 15, 2021 - A null pointer constant may be implicitly converted to any pointer type; such conversion results in the null pointer value of that type. If a null pointer constant has integer type, it may be converted to a prvalue of type std::nullptr_t.
🌐
Quora
quora.com › Whats-the-difference-between-NULL-and-nullptr-in-C++
What's the difference between NULL and nullptr in C++? - Quora
Answer (1 of 13): NULL is a “manifest constant” (a [code ]#define[/code] of C) that’s actually an integer that can be assigned to a pointer because of an implicit conversion. nullptr is a keyword representing a value of self-defined type, that can convert into a pointer, but not into integers.
Find elsewhere
🌐
cppreference.com
en.cppreference.com › cpp › language › nullptr
nullptr, the pointer literal (since C++11) - cppreference.com
The keyword nullptr denotes the pointer literal. It is a prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › extensions › nullptr-cpp-component-extensions
nullptr (C++/CLI and C++/CX) | Microsoft Learn
June 25, 2025 - The __nullptr keyword is a Microsoft-specific keyword that has the same meaning as nullptr, but applies to only native code. If you use nullptr with native C/C++ code and then compile with the /clr compiler option, the compiler cannot determine whether nullptr indicates a native or managed null pointer value.
Top answer
1 of 1
109

In C++11 and beyond, a pointer that is ==NULL will also ==nullptr and vice versa.

Uses of NULL other than comparing with a pointer (like using it to represent the nul byte at the end of a string) won't work with nullptr.

In some cases, NULL is #define NULL 0, as the integer constant 0 is special-cased in C and C++ when you compare it with pointers. This non-type type information causes some problems in both C and C++, so in C++ they decided to create a special type and value that does the same thing in the "proper" use cases, and reliably fails to compile in most of the "improper" use cases.

Insofar as your C++ implementation is compatible with the C implementation you are interoping with (very rare for this not to be true), everything should work.


To be very clear, if ptr is any kind of pointer, then the following expressions are equivalent in C++:

ptr == nullptr
ptr == NULL
ptr == 0
!ptr

As are the following:

ptr = nullptr
ptr = NULL
ptr = 0

and if X is some type, so are the following statements:

X* ptr = nullptr;
X* ptr = NULL;
X* ptr = 0;

nullptr differs when you pass it to a template function that deduces type (NULL or 0 become an int unless passed to an argument expecting a pointer, while nullptr remains a nullptr_t), and when used in some contexts where nullptr won't compile (like char c = NULL;) (note, not char* c=NULL;)

Finally, literally:

NULL == nullptr

is true.

The NULL constant gets promoted to a pointer type, and as a pointer it is a null pointer, which then compares equal to nullptr.


Despite all this, it isn't always true that:

 foo(NULL)

and

 foo(nullptr)

do the same thing.

void bar(int) { std::cout << "int\n"; }
void bar(void*) { std::cout << "void*\n"; }
template<class T>
void foo(T t) { bar(t); }
foo(NULL);
foo(nullptr);

this prints int for NULL and void* for nullptr.

🌐
Sololearn
sololearn.com › en › Discuss › 3097671 › null-vs-nullptr
NULL vs nullptr | Sololearn: Learn to code for FREE!
The difference is that NULL is an integer, nullptr is a pointer type. It is a crucial difference when dealing with (resolving) method overloads. They are not interchangeable*). When you talk pointer, always use nullptr.
🌐
Medium
medium.com › @weidagang › modern-c-nullptr-fa494808d31a
Modern C++: nullptr. Saying Goodbye to NULL | by Dagang Wei | Medium
June 24, 2024 - nullptr is a keyword in C++ that explicitly represents a null pointer value. It has its own distinct type (std::nullptr_t), ensuring type safety and eliminating the ambiguity associated with NULL and 0. int* ptr = nullptr; // Clear indication ...
🌐
Scaler
scaler.com › home › topics › what is nullptr in c++?
What is nullptr in C++? - Scaler Topics
May 4, 2023 - The nullptr is prvalue i.e., pure rvalue which means you cannot take its address of it using &. The nullptr denotes pointer literals which are of type nullptr_t. There will be no ambiguity between overloaded sets while calling functions. if(ptr == nullptr) rather than (ptr == 0), the code becomes ...
🌐
Learn C++
learncpp.com › cpp-tutorial › null-pointers
12.8 — Null pointers – Learn C++
August 12, 2015 - In the above example, we use assignment ... making ptr2 a null pointer. ... Use nullptr when you need a null pointer literal for initialization, assignment, or passing a null pointer to a function. Dereferencing a null pointer results in undefined behavior · Much like dereferencing a dangling ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › nullptr
nullptr | Microsoft Learn
August 3, 2021 - Access to this page requires authorization. You can try changing directories. ... The nullptr keyword specifies a null pointer constant of type std::nullptr_t, which is convertible to any raw pointer type.
🌐
Quora
quora.com › Considering-“ptr”-is-a-pointer-is-there-a-difference-between-“-ptr”-and-“NULL-ptr”
Considering “ptr” is a pointer, is there a difference between “!ptr” and “NULL == ptr”? - Quora
Answer (1 of 5): Yes both are congruent to each other. * [code ]ptr == NULL [/code]is more commonly used way than[code ] NULL == ptr[/code]. Ref. Is there any difference between (null != x) and (x != null) In contradiction to the above statement, I came across an interesting article on the si...
🌐
Reddit
reddit.com › r/learnprogramming › [c++] difference between null and nullptr
r/learnprogramming on Reddit: [C++] Difference between null and nullptr
December 12, 2018 -

Flocked straight here because the toxic stack overflow has the biggest trigger finger when it comes to marking posts as duplicates.

But anywho.

I had an examination recently, and I remember one of the theory questions asking what the difference is between null and nullptr in c++. I was unsure and didn't know how to answer it, for future reference could someone please explain to me what differs between the two.

Top answer
1 of 2
2
They mean the same thing. nullptr is part of the language (as of c++11), whereas NULL was just a macro that turns into the number 0. 90% of the time, they're totally interchangeable. Good modern C++ code should always use nullptr, but older code will use NULL and it will work fine. There are a few cases where nullptr is better. Suppose you have a function that can take either an int or a pointer: void Create(int count); // Creates this many enemies void Create(Enemy* clone); // Create one enemy, with an optional enemy to clone (may be nullptr) If you call Create(NULL), it will actually call the first version, which is not what you want. That's because NULL is just 0, which is an int. If you call Create(nullptr), it knows you want to call the second version.
2 of 2
1
nullptr - A literal of type std::nullptr_t, used to represent a null pointer, or a pointer that doesn't point to anything. NULL - Post C++11, this is a macro for nullptr. Before C++11, this was the constant 0. In C it could also be (void*)0. Regardless, it's also intended to represent a null pointer. You should generally use nullptr instead whenever possible. 0 - In C, and therefore C++ for compatibility reasons, assigning this to a pointer will cause the pointer to be null, meaning it doesn't point to anything. The actual value stored in the pointer need not be the literal bits 0x0000000 (historically, many systems have used other representations for null), but that's what it is on most modern systems. '\0' aka "nul" - this is the null character, a special non-printable control character, often used to designate the end of a string. std::nullopt - used to represent the null case in a std::optional. This is a nullable type, meaning it can have a state where it holds no value at all (which is different than 0, which is itself a value).