← Back to C Course | Chapter 7: Pointers | Lesson 7 of 9

C Null Pointer

A null pointer points to nothing, like an empty address on an envelope. Checking for it before use keeps your program from crashing.
Syntax
c
data_type *pointer_name = NULL;

if (pointer_name != NULL) {
  // safe to use *pointer_name
}

What is a Null Pointer?

It's the conventional 'this pointer intentionally points nowhere' signal in C, used both as an initial safe default and as a sentinel value that functions like malloc() and fopen() return to indicate failure.

Example: What is a Null Pointer?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Print formatted text to the screen
	printf("%d", ptr == NULL);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Initializing a Null Pointer

NULL is actually just defined as the integer 0 cast to a pointer type in most implementations, so int *ptr = NULL and int *ptr = 0 are functionally equivalent, though writing NULL communicates intent more clearly to readers.

Example: Initializing a Null Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Print formatted text to the screen
	printf("%d", ptr == 0);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Safe Pointer Checking

This is defensive programming 101 in C -- since the language has no automatic null-checking, forgetting this check is one of the most common causes of segmentation faults, especially right after a malloc() or fopen() call that might have failed.

Example: Safe Pointer Checking

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Check whether ptr != NULL
	if (ptr != NULL) {
		// Print formatted text to the screen
		printf("%d", *ptr);
	} else {
		// Print formatted text to the screen
		printf("Cannot dereference a null pointer");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Null Pointer vs. Uninitialized Pointer

An uninitialized pointer is far more dangerous than a null one because it might accidentally point at valid-looking memory and appear to work, masking the bug until it corrupts something important -- always initialize pointers explicitly, even to NULL.

Example: Null Pointer vs. Uninitialized Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *safePtr = NULL;
	// Print formatted text to the screen
	printf("%d", safePtr == NULL);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Null Pointers as Return Values

This convention lets calling code distinguish 'operation succeeded, here's your result' from 'operation failed' using a single return value, which is why checking a function's return pointer against NULL before using it is standard practice.

Example: Null Pointers as Return Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int* find(int value) {
	// Check whether value != 5
	if (value != 5) {
		// Send NULL back to the caller
		return NULL;
	}
	static int found = 5;
	// Send &found back to the caller
	return &found;
}
// Program execution starts in main()
int main() {
	int *result = find(3);
	// Print formatted text to the screen
	printf("%d", result == NULL);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Dereferencing a null pointer, which crashes the program.
  2. Using an uninitialized pointer instead of setting it to NULL.
  3. Not checking a pointer for NULL before using it.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.