...

/

nullptr Instead of 0 or NULL

nullptr Instead of 0 or NULL

In this lesson, we will learn how new null pointer nullptr cleans up in C++ with the ambiguity of the number 0 and the macro NULL.

The Number 0

The issue with the literal 0 is that it can be either the null pointer (void*)0 or the number 0 depending on the context of the problem in question.

Therefore, programs using the number 0 should be confusing.

Press + to interact
// null.cpp
#include <iostream>
#include <typeinfo>
int main(){
std::cout << std::endl;
int a = 0;
int* b = 0;
auto c = 0;
std::cout << typeid(c).name() << std::endl;
auto res = a + b + c;
std::cout << "res: " << res << std::endl;
std::cout << typeid(res).name() << std::endl;
std::cout << std::endl;
}

The question remains, what is the data type of variable c in line 12 and of variable res in line 15?

The variable c is of type int and the variable res is of type pointer to int: int*. Pretty simple, right? The expression a + b + c in line 15 is pointer arithmetic.

The macro NULL

The issue with the null pointer NULL is that it implicitly ...

Access this course and 1400+ top-rated courses and projects.