Understanding nullptr in C++ Last Updated : 05 Apr, 2024 Comments Improve Suggest changes Like Article Like Report Consider the following C++ program that shows problem with NULL (need of nullptr) CPP // C++ program to demonstrate problem with NULL #include <bits/stdc++.h> using namespace std; // function with integer argument void fun(int N) { cout << "fun(int)"; return;} // Overloaded function with char pointer argument void fun(char* s) { cout << "fun(char *)"; return;} int main() { // Ideally, it should have called fun(char *), // but it causes compiler error. fun(NULL); } Output: 16:13: error: call of overloaded 'fun(NULL)' is ambiguous fun(NULL); What is the problem with above program? NULL is typically defined as (void *)0 and conversion of NULL to integral types is allowed. So the function call fun(NULL) becomes ambiguous. CPP // This program compiles (may produce warning) #include<stdio.h> int main() { int x = NULL; } How does nullptr solve the problem? In the above program, if we replace NULL with nullptr, we get the output as "fun(char *)".nullptr is a keyword that can be used at all places where NULL is expected. Like NULL, nullptr is implicitly convertible and comparable to any pointer type. Unlike NULL, it is not implicitly convertible or comparable to integral types. CPP // This program does NOT compile #include<stdio.h> int main() { int x = nullptr; } Output: Compiler ErrorAs a side note, nullptr is convertible to bool. CPP // This program compiles #include<iostream> using namespace std; int main() { int *ptr = nullptr; // Below line compiles if (ptr) { cout << "true"; } else { cout << "false"; } } Output: false There are some unspecified things when we compare two simple pointers but comparison between two values of type nullptr_t is specified as, comparison by <= and >= return true and comparison by < and > returns false and comparing any pointer type with nullptr by == and != returns true or false if it is null or non-null respectively. C++ // C++ program to show comparisons with nullptr #include <bits/stdc++.h> using namespace std; // Driver program to test behavior of nullptr int main() { // creating two variables of nullptr_t type // i.e., with value equal to nullptr nullptr_t np1, np2; // <= and >= comparison always return true if (np1 >= np2) cout << "can compare" << endl; else cout << "can not compare" << endl; // Initialize a pointer with value equal to np1 char *x = np1; // same as x = nullptr (or x = NULL // will also work) if (x == nullptr) cout << "x is null" << endl; else cout << "x is not null" << endl; return 0; } Output : can comparex is null nullptr in C++ Visit Course Comment More infoAdvertise with us K kartik Improve Article Tags : C++ cpp-pointer Practice Tags : CPP Explore C++ Programming Language 5 min read C++ OverviewIntroduction to C++ Programming Language 3 min read Features of C++ 5 min read History of C++ 7 min read Interesting Facts about C++ 2 min read Setting up C++ Development Environment 8 min read Difference between C and C++ 3 min read C++ BasicsUnderstanding First C++ Program 4 min read C++ Basic Syntax 4 min read C++ Comments 3 min read Tokens in C 4 min read C++ Keywords 2 min read Difference between Keyword and Identifier in C 3 min read C++ Variables and ConstantsC++ Variables 4 min read Constants in C 4 min read Scope of Variables in C++ 7 min read Storage Classes in C++ with Examples 6 min read Static Keyword in C++ 5 min read C++ Data Types and LiteralsC++ Data Types 7 min read Literals in C 4 min read Derived Data Types in C++ 4 min read User Defined Data Types in C++ 4 min read Data Type Ranges and Their Macros in C++ 3 min read C++ Type Modifiers 4 min read Type Conversion in C++ 4 min read Casting Operators in C++ 5 min read C++ OperatorsOperators in C++ 9 min read C++ Arithmetic Operators 4 min read Unary Operators in C 5 min read Bitwise Operators in C 6 min read Assignment Operators in C 4 min read C++ sizeof Operator 3 min read Scope Resolution Operator in C++ 4 min read C++ Input/OutputBasic Input / Output in C++ 5 min read cin in C++ 4 min read cout in C++ 2 min read Standard Error Stream Object - cerr in C++ 2 min read Manipulators in C++ 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if ) 7 min read C++ if Statement 3 min read C++ if else Statement 3 min read C++ if else if Ladder 3 min read Switch Statement in C++ 5 min read Jump statements in C++ 4 min read C++ Loops 7 min read for Loop in C++ 6 min read Range-Based for Loop in C++ 3 min read C++ While Loop 3 min read C++ do while Loop 4 min read C++ FunctionsFunctions in C++ 8 min read return Statement in C++ 4 min read Parameter Passing Techniques in C 3 min read Difference Between Call by Value and Call by Reference in C 4 min read Default Arguments in C++ 5 min read Inline Functions in C++ 6 min read Lambda Expression in C++ 4 min read C++ Pointers and ReferencesPointers and References in C++ 5 min read C++ Pointers 8 min read Dangling, Void , Null and Wild Pointers in C 6 min read Applications of Pointers in C 4 min read Understanding nullptr in C++ 3 min read References in C++ 5 min read Can References Refer to Invalid Location in C++? 2 min read Pointers vs References in C++ 5 min read Passing By Pointer vs Passing By Reference in C++ 5 min read When do we pass arguments by pointer? 5 min read Like