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. Answer from mredding on reddit.com
🌐
Cppreference
en.cppreference.com › w › c › language › nullptr.html
Predefined null pointer constant (since C23) - cppreference.com
It is a non-lvalue of type nullptr_t. nullptr can be converted to a pointer types or bool, where the result is the null pointer value of that type or false respectively. ... Demonstrates that a copy of nullptr can also be used as a null pointer constant. ... #include <stddef.h> #include <stdio.h> ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
We just have to assign the NULL value. Strictly speaking, NULL expands to an implementation-defined null pointer constant which is defined in many header files such as “stdio.h”, “stddef.h”, “stdlib.h” etc. Following are some most common uses of the NULL pointer in C:
Published   January 10, 2025
Discussions

What is the difference between NULL and nullptr when using them for something like a Binary Search Tree? Are those interchangeable?
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. More on reddit.com
🌐 r/cpp_questions
5
8
June 29, 2021
Is C NULL equal to C++11 nullptr - Stack Overflow
I like to use nullptr instead of NULL. Now I call a C function (from libjansson in this case). NULL in C is implementation defined. For nullptr I found that "A null pointer constant is an integral More on stackoverflow.com
🌐 stackoverflow.com
Trying to understand NULL pointers
NULL is equivalent to 0 and equivalent to nullptr (in C++). It's an address that is "universally invalid." You can store 0x2093704802934 in a pointer and it will be invalid too (I'm sure) but it's not a convention. NULL is this convention that says "the pointer is not valid" and/or "the pointer hasn't been initialized yet" or "the memory pointed to has been released and I have invalidated the pointer by setting it to NULL." You store this value in a "pointer", like "int* s = 0" to show that it is not pointing to anything. And in your code you will check whether the pointer (s in my example) is equal to NULL (invalid) or not (valid). First you create a pointer, then you initialize it, then you use it, then you release it. More on reddit.com
🌐 r/learnprogramming
18
1
December 30, 2021
Using Null pointers - C++ Forum
You can think of NULL as a special pointer, which usually points to memory address 0. In the current standard of C++ (which is C++11 of year 2011), instead of NULL you should use nullptr. More on cplusplus.com
🌐 cplusplus.com
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › extensions › nullptr-cpp-component-extensions
nullptr (C++/CLI and C++/CX) | Microsoft Learn
June 25, 2025 - If you use nullptr with native ... value. To make your intention clear to the compiler, use nullptr to specify a managed value or __nullptr to specify a native value....
🌐
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
6
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).
🌐
Medium
medium.com › @pauljlucas › nullptr-in-c23-571782008dad
nullptr in C23. The new nullptr keyword in C23. | by Paul J. Lucas | Medium
July 8, 2025 - Like void* and 0, nullptr implicitly converts to any type of pointer; unlike NULL, it’s guaranteed to be a pointer. Why not just fix NULL, i.e., always define it to be ((void*)0)? The proposal mentions it, but doesn’t elaborate. My guess is that if your code continues to use NULL, you may ...
🌐
cppreference.com
en.cppreference.com › cpp › language › nullptr
nullptr, the pointer literal (since C++11) - cppreference.com
August 12, 2024 - #include <cstddef> #include <iostream> template<class T> constexpr T clone(const T& t) { return t; } void g(int*) { std::cout << "Function g called\n"; } int main() { g(nullptr); // Fine g(NULL); // Fine g(0); // Fine g(clone(nullptr)); // Fine // g(clone(NULL)); // ERROR: non-literal zero cannot be a null pointer constant // g(clone(0)); // ERROR: non-literal zero cannot be a null pointer constant }
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:

Copyptr = nullptr
ptr = NULL
ptr = 0

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

CopyX* 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:

CopyNULL == 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:

Copy foo(NULL)

and

Copy foo(nullptr)

do the same thing.

Copyvoid 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.

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › understanding-nullptr-c
Understanding nullptr in C++ - GeeksforGeeks
In C++, NULL was traditionally used to represent null pointers. However, its use can lead to ambiguity in function overloading and unintended type conversions. To overcome these issues, C++11 introduced nullptr, a type-safe keyword that clearly represents a null pointer and eliminates ambiguity ...
Published   1 week ago
🌐
DEV Community
dev.to › pauljlucas › nullptr-in-c23-1cn6
nullptr in C23 - DEV Community
July 8, 2025 - Like void* and 0, nullptr implicitly converts to any type of pointer; unlike NULL, it’s guaranteed to be a pointer. Why not just fix NULL, i.e., always define it to be ((void*)0)? The proposal mentions it, but doesn’t elaborate. My guess is that if your code continues to use NULL, you may ...
🌐
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 - As a reminder, since C++11, NULL can be either an integer literal with value zero, or a prvalue of type std::nullptr_t. Because of this ambiguity, I recommend switching exclusively to nullptr. nullptr will make your code less error-prone than relying on the implementation-defined NULL. But your code will not blow up if you stick to NULL. Your existing code is probably fine to leave as it is. ... Want to use C++, but worried about how much it relies on dynamic memory allocations?
🌐
Quora
quora.com › What-is-the-use-of-nullptr-in-C
What is the use of nullptr in C++? - Quora
In C/C++, zero evaluates to false, so short statements like if(Ptr) will execute their if-block if the pointer has been assigned, otherwise won't if the pointer is nullptr. ... It's a convenient constant for an invalid/unassigned pointer.
🌐
Reddit
reddit.com › r/learnprogramming › trying to understand null pointers
r/learnprogramming on Reddit: Trying to understand NULL pointers
December 30, 2021 -

Hello all again,

I have another stupid question here lol, so I'm trying to wrap my head around NULL. Im currently under the impression that NULL is a built in constant that has a value of zero, but what does that actually mean? When would it be appropriate to use null? If someone could explain it in layman's terms that would be super helpful!

🌐
Scaler
scaler.com › home › topics › what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - To avoid programming errors, use a NULL pointer as a best practice. Before using a pointer, make it a practice to assign it a value. Don't utilize the pointer before it's been initialized. If you don't have a valid memory location to store in a pointer variable, set it to NULL instead. Check for a NULL value before utilizing a pointer in any of your function code.
🌐
Cplusplus
cplusplus.com › forum › beginner › 123277
Using Null pointers - C++ Forum
You can think of NULL as a special pointer, which usually points to memory address 0. In the current standard of C++ (which is C++11 of year 2011), instead of NULL you should use nullptr.
🌐
Medium
medium.com › @weidagang › modern-c-nullptr-fa494808d31a
Modern C++: nullptr. Saying Goodbye to NULL | by Dagang Wei | Medium
June 24, 2024 - It avoids potential ambiguities and makes the intent clearer. Use nullptr Consistently: In your modern C++ projects, make nullptr your default choice for representing null pointers.
🌐
Open-std
open-std.org › jtc1 › sc22 › wg14 › www › docs › n3042.htm
Introduce the nullptr constant
July 22, 2022 - Suggest to the editors to introduce the term “default initialization”. v5 Add nullptr_t to the scalar types. Now most of the desired features follow directly. ... have nullptr_t as a complete object type that has the same representation as void* and char* but only one value, nullptr · for “Boolean” use cases of nullptr_t only explicitly formulate conversion to bool and test for equality
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:

Copyvoid 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
Copynamespace 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
🌐
Reddit
reddit.com › r/c_programming › [deleted by user]
[deleted by user] : r/C_Programming
July 23, 2025 - The surprising thing is: nullptr isn't actually a pointer, but some other type (nulltpr_t was introduced for that purpose) with only one possible value, and the guarantee that the representation is the same as that of (void *)0. What makes nullptr work as a drop-in replacement for NULL is the rule to be implicitly convertible to any pointer type, and the conversion will result in the null-pointer of that type. Also, like an actual null pointer, it evaluates to false in a boolean context. Note this is quite similar to the rules for a 0 of any integer type. You could actually use just 0 almost anywhere you see NULL or nullptr, because 0 as well is implicitly convertible to a pointer and the result is guaranteed to be the null pointer.
🌐
Codidact
software.codidact.com › posts › 292718
Why not call nullptr NULL? - Software Development - Codidact
Got questions? Got answers? Got code you'd like someone to review? Please join us. ... In C++11 the nullptr keyword was added as a more type safe null pointer constant, since the previous common definition of NULL as 0 has some problems.