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.
Videos
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;
}
ISO C 23 now has nullptr, as well as the nullptr_t type. The proposal that introduced it has some rationale.
No, C still uses NULL for a null pointer.
C++ needs a dedicated null pointer literal because it has overloading and template type deduction. These features get confused by NULL, which expands to 0 (or something like that) in C++. However, the risk of such confusion in C is small (maybe _Generic can get confused by this), and in addition, C can use (void*)0 for a null pointer, which mitigates this risk even more.