In my experience, tests of the form if (ptr) or if (!ptr) are preferred. They do not depend on the definition of the symbol NULL. They do not expose the opportunity for the accidental assignment. And they are clear and succinct.

Edit: As SoapBox points out in a comment, they are compatible with C++ classes such as unique_ptr, shared_ptr, auto_ptr that are objects that act as pointers and which provide a conversion to bool to enable exactly this idiom. For these objects, an explicit comparison to NULL would have to invoke a conversion to pointer which may have other semantic side effects or be more expensive than the simple existence check that the bool conversion implies.

I have a preference for code that says what it means without unneeded text. if (ptr != NULL) has the same meaning as if (ptr) but at the cost of redundant specificity. The next logical thing is to write if ((ptr != NULL) == TRUE) and that way lies madness. The C language is clear that a boolean tested by if, while or the like has a specific meaning of non-zero value is true and zero is false. Redundancy does not make it clearer.

Answer from RBerteig on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
It is a valid operation in pointer arithmetic to check whether the pointer is NULL. We just have to use isequal to operator ( == ) as shown below: ... The above equation will be true if the pointer is NULL, otherwise, it will be false.
Published   January 10, 2025
Top answer
1 of 6
30

In C and C++, pointers are inherently unsafe, that is, when you dereference a pointer, it is your own responsibility to make sure it points somewhere valid; this is part of what "manual memory management" is about (as opposed to the automatic memory management schemes implemented in languages like Java, PHP, or the .NET runtime, which won't allow you to create invalid references without considerable effort).

A common solution that catches many errors is to set all pointers that don't point to anything as NULL (or, in correct C++, 0), and checking for that before accessing the pointer. Specifically, it is common practice to initialize all pointers to NULL (unless you already have something to point them at when you declare them), and set them to NULL when you delete or free() them (unless they go out of scope immediately after that). Example (in C, but also valid C++):

void fill_foo(int* foo) {
    *foo = 23; // this will crash and burn if foo is NULL
}

A better version:

void fill_foo(int* foo) {
    if (!foo) { // this is the NULL check
        printf("This is wrong\n");
        return;
    }
    *foo = 23;
}

Without the null check, passing a NULL pointer into this function will cause a segfault, and there is nothing you can do - the OS will simply kill your process and maybe core-dump or pop up a crash report dialog. With the null check in place, you can perform proper error handling and recover gracefully - correct the problem yourself, abort the current operation, write a log entry, notify the user, whatever is appropriate.

2 of 6
8

The other answers pretty much covered your exact question. A null check is made to be sure that the pointer you received actually points to a valid instance of a type (objects, primitives, etc).

I'm going to add my own piece of advice here, though. Avoid null checks. :) Null checks (and other forms of Defensive Programming) clutter code up, and actually make it more error prone than other error-handling techniques.

My favorite technique when it comes to object pointers is to use the Null Object pattern. That means returning a (pointer - or even better, reference to an) empty array or list instead of null, or returning an empty string ("") instead of null, or even the string "0" (or something equivalent to "nothing" in the context) where you expect it to be parsed to an integer.

As a bonus, here's a little something you might not have known about the null pointer, which was (first formally) implemented by C.A.R. Hoare for the Algol W language in 1965.

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

🌐
Quora
quora.com › How-do-I-check-if-a-pointer-is-null-in-C
How to check if a pointer is null in C - Quora
Answer (1 of 5): From the top: #ifndef NULL #define NULL (*)(0) #endif /**********************************/ /* we just have set a value for a */ /* NULL pointer. From ancient times */ /* this points to physical and or */ /* logical address 0 *************/ /******************************...
🌐
Unstop
unstop.com › home › blog › null pointer in c | a detailed explanation with examples
Null Pointer In C | A Detailed Explanation With Examples // Unstop
May 3, 2024 - We initialize them both to NULL, which indicates that neither pointer currently points to any valid memory location. We then use two separate if-statements to check if each pointer is equal to NULL, using the relational equality operator.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › c programming languages › how to check null in c: 7 steps (with pictures) - wikihow
How to Check Null in C: 7 Steps (with Pictures) - wikiHow
June 9, 2025 - It's common practice to set newly created or newly freed pointers to NULL to make sure you don't use this unhelpful address by accident. Avoid this mistake: char *ptr; if(ptr == NULL) { //This will return FALSE.
Top answer
1 of 9
98

I always think simply if(p != NULL){..} will do the job.

It will.

2 of 9
43

First, to be 100% clear, there is no difference between C and C++ here. And second, the Stack Overflow question you cite doesn't talk about null pointers; it introduces invalid pointers; pointers which, at least as far as the standard is concerned, cause undefined behavior just by trying to compare them. There is no way to test in general whether a pointer is valid.

In the end, there are three widespread ways to check for a null pointer:

if ( p != NULL ) ...

if ( p != 0 ) ...

if ( p ) ...

All work, regardless of the representation of a null pointer on the machine. And all, in some way or another, are misleading; which one you choose is a question of choosing the least bad. Formally, the first two are indentical for the compiler; the constant NULL or 0 is converted to a null pointer of the type of p, and the results of the conversion are compared to p. Regardless of the representation of a null pointer.

The third is slightly different: p is implicitly converted to bool. But the implicit conversion is defined as the results of p != 0, so you end up with the same thing. (Which means that there's really no valid argument for using the third style—it obfuscates with an implicit conversion, without any offsetting benefit.)

Which one of the first two you prefer is largely a matter of style, perhaps partially dictated by your programming style elsewhere: depending on the idiom involved, one of the lies will be more bothersome than the other. If it were only a question of comparison, I think most people would favor NULL, but in something like f( NULL ), the overload which will be chosen is f( int ), and not an overload with a pointer. Similarly, if f is a function template, f( NULL ) will instantiate the template on int. (Of course, some compilers, like g++, will generate a warning if NULL is used in a non-pointer context; if you use g++, you really should use NULL.)

In C++11, of course, the preferred idiom is:

if ( p != nullptr ) ...

, which avoids most of the problems with the other solutions. (But it is not C-compatible:-).)

Find elsewhere
Top answer
1 of 9
17

Invalid null pointers can either be caused by programmer error or by runtime error. Runtime errors are something a programmer can't fix, like a malloc failing due to low memory or the network dropping a packet or the user entering something stupid. Programmer errors are caused by a programmer using the function incorrectly.

The general rule of thumb I've seen is that runtime errors should always be checked, but programmer errors don't have to be checked every time. Let's say some idiot programmer directly called graph_get_current_column_color(0). It will segfault the first time it's called, but once you fix it, the fix is compiled in permanently. No need to check every single time it's run.

Sometimes, especially in third party libraries, you'll see an assert to check for the programmer errors instead of an if statement. That allows you to compile in the checks during development, and leave them out in production code. I've also occasionally seen gratuitous checks where the source of the potential programmer error is far removed from the symptom.

Obviously, you can always find someone more pedantic, but most C programmers I know favor less cluttered code over code that is marginally safer. And "safer" is a subjective term. A blatant segfault during development is preferable to a subtle corruption error in the field.

2 of 9
13

Kernighan & Plauger, in "Software Tools", wrote that they would check everything, and, for conditions that they believed could in fact never happen, they would abort with an error message "Can't happen".

They report being rapidly humbled by the number of times they saw "Can't happen" come out on their terminals.

You should ALWAYS check the pointer for NULL before you (attempt to) dereference it. ALWAYS. The amount of code you duplicate checking for NULLs that don't happen, and the processor cycles you "waste", will be more than paid for by the number of crashes you don't have to debug from nothing more than a crash dump - if you're that lucky.

If the pointer is invariant inside a loop, it suffices to check it outside the loop, but you should then "copy" it into a scope-limited local variable, for use by the loop, that adds the appropriate const decorations. In this case, you MUST ensure that every function called from the loop body includes the necessary const decorations on the prototypes, ALL THE WAY DOWN. If you don't, or can't (because of e.g. a vendor package or an obstinate coworker), then you must check it for NULL EVERY TIME IT COULD BE MODIFIED, because sure as COL Murphy was an incurable optimist, someone IS going to zap it when you aren't looking.

If you are inside a function, and the pointer is supposed to be non-NULL coming in, you should verify it.

If you are receiving it from a function, and it is supposed to be non-NULL coming out, you should verify it. malloc() is particularly notorious for this. (Nortel Networks, now defunct, had a hard-and-fast written coding standard about this. I got to debug a crash at one point, that I traced back to malloc() returning a NULL pointer and the idiot coder not bothering to check it before he wrote to it, because he just KNEW he had plenty of memory... I said some very nasty things when I finally found it.)

🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_null_pointer.htm
NULL Pointer in C
To check for a null pointer before accessing any pointer variable so that we can perform error handling in pointer-related code. For example, dereference a pointer variable only if it's not NULL.
🌐
YouTube
youtube.com › watch
NULL Pointer | C Programming Tutorial - YouTube
An explanation of the NULL pointer value in C, including common use cases of the NULL pointer value. Source code: https://github.com/portfoliocourses/c-exam...
Published   December 18, 2022
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-if-a-variable-is-null-in-c-cplusplus
How to check if a variable is NULL in C/C++?
#include<iostream> using namespace std; int main() { // Pointer initialized to NULL int* ptr = NULL; if (ptr == NULL) { cout << "The pointer is NULL." << endl; } else { cout << "The pointer is not NULL." << endl; } // Assign some value int value = 100; ptr = &value; if (ptr != NULL) { cout ...
🌐
Scaler
scaler.com › topics › null-pointer-in-c
What is Null Pointer in C? - Scaler Topics
November 9, 2022 - A null pointer refers to the 0th ... the value NULL to indicate that it doesn't point to any variables. We add a condition after creating a pointer variable that checks if the value of the pointer is null....
🌐
LinkedIn
linkedin.com › learning › secure-coding-in-c › checking-for-null-pointers
Checking for NULL pointers - C Video Tutorial | LinkedIn Learning, formerly Lynda.com
To confirm that memory has been allocated, compare the pointer returned by a function with the NULL pointer constant. The NULL constant comparison must be used with any function that returns a pointer, such as fopen(). Confirming the pointer's ...
Published   November 25, 2019
🌐
Eskimo
eskimo.com › ~scs › cclass › notes › sx10d.html
10.4 Null Pointers
If one of the pointers in your programs points somewhere some of the time but not all of the time, an excellent convention to use is to set it to a null pointer when it doesn't point anywhere valid, and to test to see if it's a null pointer before using it. But you must use explicit code to set it to NULL, and to test it against NULL. (In other words, just setting an unused pointer variable to NULL doesn't guarantee safety; you also have to check for the null value before using the pointer.)
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c programming tutorial › null pointer in c
Null pointer in C | How Null pointer work in C with Examples
March 28, 2023 - So there is a way to check for the pointer is null or not by using if(ptr) results in 1 if the pointer is not null and if(!ptr) results in 1 when the pointer is null as we did in the above-modified program.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Codecademy
codecademy.com › docs › pointers › null pointer
C | Pointers | Null Pointer | Codecademy
February 3, 2025 - In the example, we declare an integer pointer ptr and initialize it to NULL, indicating that it doesn’t point to any valid memory location: ... The if statement checks if ptr is NULL, if true, it prints “Pointer is NULL” to the screen.
🌐
Medium
medium.com › @Code_Analysis › shall-we-check-pointer-for-null-before-calling-free-function-63a27bb2e75e
Shall we check pointer for NULL before calling free function? | by Andrey Karpov | Medium
February 6, 2024 - The code doesn’t look the way it works. Let’s expand the macro. ... The else statement refers to the second if statement and is executed when the pointer is null. Well, things don’t work as intended.