C++ nullptr Keyword
The nullptr keyword in C++ is a null pointer constant introduced in C++11. It provides a type-safe way to represent a null pointer, replacing the traditional NULL macro and the literal 0 used in earlier versions of C++.
Using nullptr eliminates ambiguity in function overloading and improves code readability and type safety when working with pointers.
Syntax
</>
Copy
data_type* pointer_name = nullptr;
- data_type
- The type of the pointer, such as
int,double, etc. - pointer_name
- The name of the pointer variable being declared.
- nullptr
- A null pointer constant representing the absence of a valid memory address.
Examples
Example 1: Initializing a Pointer with nullptr
In this example, you will learn how to use nullptr to initialize a pointer.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int* ptr = nullptr; // Initialize pointer with nullptr
if (ptr == nullptr) {
cout << "The pointer is null." << endl;
}
return 0;
}
Output:
The pointer is null.
Explanation:
- The pointer
ptris initialized withnullptr, indicating it does not point to any valid memory location. - The
ifstatement checks whetherptris null, and the corresponding message is printed.
Example 2: Using nullptr in Function Overloading
In this example, you will learn how nullptr resolves ambiguity in function overloading.
</>
Copy
#include <iostream>
using namespace std;
void print(int* ptr) {
if (ptr == nullptr) {
cout << "Null pointer passed." << endl;
} else {
cout << "Pointer value: " << *ptr << endl;
}
}
void print(int value) {
cout << "Integer value: " << value << endl;
}
int main() {
int* ptr = nullptr;
print(ptr); // Calls the first overload
int value = 42;
print(value); // Calls the second overload
return 0;
}
Output:
Null pointer passed.
Integer value: 42
Explanation:
- Two
printfunctions are defined: one accepts a pointer, and the other accepts an integer. - Using
nullptrinprint(ptr)ensures the correct function overload (pointer version) is called. - When
print(value)is called, the integer version of the function executes.
Example 3: Assigning nullptr to Reset a Pointer
In this example, you will learn how to reset a pointer using nullptr.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int value = 10;
int* ptr = &value;
cout << "Pointer before reset: " << *ptr << endl;
ptr = nullptr; // Reset the pointer
if (ptr == nullptr) {
cout << "Pointer has been reset to nullptr." << endl;
}
return 0;
}
Output:
Pointer before reset: 10
Pointer has been reset to nullptr.
Explanation:
- The pointer
ptrinitially points to the address ofvalue. - The pointer is reset using
nullptr, making it a null pointer. - The
ifcondition confirms that the pointer has been reset.
Key Points about nullptr Keyword
- The
nullptrkeyword provides a type-safe way to represent a null pointer. - It replaces older methods like
NULLor0, which could lead to ambiguity in function calls. - Using
nullptrimproves code readability and eliminates common pointer-related bugs. - It can be assigned to pointers of any type.
- Introduced in C++11,
nullptris the recommended way to handle null pointers in modern C++ code.
