PDF download Download Article PDF download Download Article

In C, NULL is a symbolic constant that always points to a nonexistent point in the memory. Although many programmers treat it as equal to 0, this is a simplification that can trip you up later on. It's best to check your pointers against NULL directly, and use 0 in other contexts. This keeps your code clean and easy to understand, since you'll know you're working with pointers every time you see NULL.

Part 1
Part 1 of 2:

Performing a Null Check

PDF download Download Article
  1. The following is the most obvious way to write a null check. We'll use ptr in this article as the name of the pointer you're checking.
    • if(ptr == NULL)
      {
         // code if pointer is NULL
      } else {
         // code if not NULL
      }
  2. Sometimes it's more convenient to test for inequality instead. No surprises here:
    • if (ptr != NULL) {
         // code if not NULL
      }
    Advertisement
  3. The main disadvantage to the PTR == NULL method is the chance that you'll accidentally type ptr = NULL instead, assigning the NULL value to that pointer. This can cause a major headache. Since testing for (in)equality treats the operands symmetrically, you can get exactly the same result by writing if (NULL == ptr) instead. This is more typo-resistant, since an accidental NULL = ptr creates a simple compile error.
    • This looks a little awkward to some programmers, but it's perfectly valid. Which approach you use just depends on personal preference, and how good your compiler is at detecting the if (ptr = NULL) error.
  4. A simple if (ptr) tests whether ptr is TRUE. It will return FALSE if ptr is NULL, or if ptr is 0. The distinction doesn't matter in many cases, but be aware that these are not identical in all architectures.[1]
    • The reverse of this is if (!ptr), which will return TRUE if ptr is FALSE.
  5. Advertisement
Part 2
Part 2 of 2:

Avoiding Mistakes

PDF download Download Article
  1. One common mistake is to assume that a newly created pointer has a NULL value. This is not true. An unassigned pointer still points to a memory address, just not one that you have specified. 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. The pointer has been assigned a valid value.
      }
    • Instead write:
      char *ptr = NULL; //This assigns the pointer to NULL
      if(ptr == NULL)
      {
      //This will return TRUE if the pointer has not been reassigned.
      }
  2. If a function can return NULL, think about whether this is a possibility, and whether that would cause problems later in your code. Here's an example of the malloc function using the null check (if (ptr)) to ensure it only handles pointers with valid values:
    • int *ptr = malloc(N * sizeof(int));
      if (ptr) {
         int i;
         for (i = 0; i < N; ++i)
            ptr[i] = i;
      }
  3. Historically, C represented NULL as the number 0 (that is, 0x00). Nowadays it can get a bit more complicated, and varies by operating system. You can usually check for NULL using ptr == 0, but there are corner cases where this can cause an issue. Perhaps more importantly, using NULL makes it obvious that you are working with pointers for other people reading your code.[2]
  4. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

Warnings

  • This article is written for C only. C++ handles NULL somewhat differently, and has the option to avoid it entirely by using references instead of pointers.
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 14 people, some anonymous, worked to edit and improve it over time. This article has been viewed 104,011 times.
How helpful is this?
Co-authors: 14
Updated: July 28, 2022
Views: 104,011
Thanks to all authors for creating a page that has been read 104,011 times.

Is this article up to date?

Advertisement