Cpp - Null Pointer Constant


The nullptr keyword is used to assign to a pointer value when it is not pointing to any useful value.

int *pBuffer = nullptr; 

The constant 0 remains valid as a null pointer value, for reasons of backward compatibility.

A nullptr value is not implicitly converted to integer types, except for bool values, where a nullptr converts to the value false.

The following code shows how to use nullptr value.


#include <iostream> 

int main() /*w  w  w .  ja v a  2  s . c  o  m*/
   int value1 = 2; 
   int value2 = 3; 
   int *pointer2 = nullptr; 
   // give pointer the address of value2 
   pointer2 = &value2; 
   // dereference the pointer and assign to value1 
   value1 = *pointer2; 
   pointer2 = 0; 
   std::cout << "value1 = " << value1 << "\n"; 
   return 0;