C NULL Pointers

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is NULL Pointers?

NULL is a null pointer constant: a pointer value that compares unequal to every valid object. Use it to initialize pointers, to signal "not found" or failure from functions, and to mark freed pointers. Dereferencing NULL is undefined behavior and usually crashes.

Remember
int *p = NULL;         /* defined in <stddef.h>, <stdio.h>, ... */
if (p == NULL) { ... }  /* or: if (!p)          */
if (p != NULL) { use(*p); }

Cheat Sheet

Keep these facts handy while you work with NULL Pointers in C.

TopicDetail
Header<stddef.h> (also stdio, stdlib, string)
Testp == NULL or !p
ReturnCommon failure value for pointer functions
Never*NULL

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Dangling Pointers.

Example 1 — Search that returns NULL when not found

Check the result before dereferencing.

C
#include <stdio.h>

int *find(int *a, int n, int key) {
    for (int i = 0; i < n; i++) {
        if (a[i] == key) return &a[i];
    }
    return NULL;
}

int main(void) {
    int data[] = {4, 8, 15};
    int *r = find(data, 3, 8);

    if (r) printf("found %d at index %td\n", *r, r - data);
    r = find(data, 3, 99);
    if (!r) printf("99 not found\n");
    return 0;
}

How It Works

  1. A hit returns the element's address.
  2. A miss returns NULL.
  3. if (r) guards the dereference; r - data converts the pointer to an index.

Edge Cases & Pitfalls

Check these before relying on NULL Pointers in real programs.

crash

Unchecked malloc

malloc returns NULL on failure. Check it before writing.

zero

Assuming NULL is 0 bytes

The null pointer may not have an all-zero bit pattern on exotic platforms. Always use NULL or 0 in pointer contexts, never memset.

Key Takeaways

  • Init: set unused pointers to NULL.
  • Check: before every dereference that may fail.
  • Signal: "not found" or "error".

One line: NULL is a null pointer constant: a pointer value that compares unequal to every valid object.

Frequently Asked Questions

NULL is a macro for a null pointer constant, a pointer value that does not point to any object or function.
The behavior is undefined; on most systems the program crashes with a segmentation fault.
Continue with "C Dangling Pointers" next. Avoid pointers to memory that no longer exists. The C Pointers hub lists every topic in order.

Next: Dangling Pointers

Avoid pointers to memory that no longer exists.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful