How to check if a pointer is NULL in C++

In C++, pointers are not safe inherently. When we dereference a pointer, it is our responsibility to ensure that it points at a valid address.

In this article, we will discuss three methods to check if a pointer is NULL in C++.

Comparison with nullptr

In C++, we can initialize a pointer by assigning the value ‘0’ or the literal ‘nullptr’.

Note: Modern C++ suggests that we should not initialize the pointers to 0. It can lead to different results when we do function overloading. In this method, we will check if the pointer is not equal to nullptr. If the condition is true, then the statements will be executed.

Code:

Output:

It is not a NULL Pointer.

Comparison with 0

In C++, there is constant NULL which has a value of zero. A pointer can be assigned to 0 or NULL. It is not recommended to assign 0 to a pointer because it can give undesired results. But, we can still check if a pointer is null or not by comparing it with 0.

Code:

Output:

It is not a NULL Pointer

Using the value of pointer as a condition

In C++, if you use a null pointer in logical expression, then they will be evaluated as false. We can pass the given pointer in the if condition to check if it is null or not.

Note: Sometimes dereferencing a null pointer can lead to undesired results and behaviour which can also terminate the program abnormally.

Code:

Output:

It is a NULL Pointer
It is not a NULL Pointer

Conclusion

In this article, we discussed the methods to check if the pointer is null. We should check if the pointer is null for handling the errors in the pointers code. For example, we will dereference a pointer only if it is not NULL.

Related Posts

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.