A null pointer in C is a pointer that does not point to any valid object or memory location. It is used to represent the absence of a meaningful address. In practical programming, null pointers are extremely important because they provide a safe and explicit way to say, “this pointer is not currently pointing anywhere useful.”
Many pointer-related bugs in C happen because programmers confuse a null pointer with an uninitialized pointer or with a dangling pointer. These are not the same things. In this article, we will understand null pointer in C clearly, learn how it is declared and used, why it is useful, how it differs from other bad pointer states, and what safe practices should be followed.
What is Null Pointer in C?
A null pointer in C is a pointer that is assigned the special null value, meaning it does not point to a valid object or function. It is used intentionally to show that the pointer is empty or not yet connected to valid memory.
In simple words, a null pointer means “no valid target.”
A null pointer is an intentionally empty pointer, not a random one and not an old invalid one.
How to Declare a Null Pointer in C
A pointer becomes a null pointer when it is assigned NULL.
int *ptr = NULL;Here, ptr does not point to any valid integer variable. It is deliberately initialized to the null state.
Why Null Pointer is Used in C
- to indicate that a pointer currently points to nothing
- to initialize pointers safely before assigning real addresses
- to detect missing or invalid object references
- to avoid using garbage addresses by accident
- to check whether memory allocation or lookup succeeded
Null pointers are useful because they make pointer state explicit instead of leaving it unknown.
Example of Null Pointer in C
The following example shows a simple null pointer declaration and check.
#include <stdio.h>
int main(void)
{
int *ptr = NULL;
if (ptr == NULL)
{
printf("Pointer is null\n");
}
return 0;
}This program safely checks the pointer before attempting to use it.
Can We Dereference a Null Pointer in C?
No. Dereferencing a null pointer is invalid and leads to undefined behavior. It often causes a crash because the pointer does not refer to a valid object.
This is unsafe:
int *ptr = NULL;
printf("%d\n", *ptr);A null pointer should always be checked before any dereference attempt.
Difference Between Null Pointer and Wild Pointer in C
A null pointer is not the same as a wild pointer.
| Point | Null pointer | Wild pointer |
|---|---|---|
| Meaning | Intentionally points to no valid object | Uninitialized or unpredictable pointer |
| State | Known and explicit | Unknown and unsafe |
| Safety | Safe if checked | Dangerous even before use |
A wild pointer contains an indeterminate address. A null pointer contains a deliberate “no valid address” state.
Difference Between Null Pointer and Dangling Pointer in C
This distinction is also very important.
| Point | Null pointer | Dangling pointer |
|---|---|---|
| Meaning | Points to nothing valid intentionally | Points to memory that used to be valid |
| Value | Special null value | Usually old address |
| Risk | Easier to check | Can look valid and be more deceptive |
A dangling pointer is broken because its old target is gone. A null pointer is intentionally empty.
Null Pointer with malloc() in C
Null pointers are commonly used when checking dynamic memory allocation.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *ptr = (int *)malloc(sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
*ptr = 100;
printf("%d\n", *ptr);
free(ptr);
return 0;
}If allocation fails, malloc() returns NULL. That is why null checks are important in memory-related code.
Benefits of Initializing Pointers to NULL
- prevents accidental use of garbage addresses
- makes program logic easier to reason about
- helps with debugging pointer-related conditions
- allows clean checks before dereferencing
- reduces the risk of wild pointer bugs
Initializing pointers to NULL is often one of the simplest safe habits in C.
Common Mistakes with Null Pointer in C
- dereferencing a null pointer without checking
- assuming null pointer and wild pointer are the same
- forgetting to check allocation failure after
malloc() - thinking null pointer automatically becomes valid later
- not resetting pointers to
NULLafter freeing memory when that pattern is useful
| Mistake | Problem | Better practice |
|---|---|---|
| Dereferencing without check | Can crash the program | Always test pointer validity first |
Ignoring malloc() result | May use null pointer by accident | Check for NULL before use |
| Confusing pointer states | Leads to wrong debugging assumptions | Separate null, wild, and dangling cases clearly |
Best Practices for Null Pointer in C
- Initialize pointers to
NULLwhen they do not yet point to a valid object. - Check pointers before dereferencing when null is possible.
- Check dynamic memory allocation results carefully.
- Use
NULLto represent “no valid target” explicitly. - Do not confuse null pointers with dangling or wild pointers.
FAQs
What is null pointer in C?
A null pointer in C is a pointer assigned the special null value to represent that it does not point to any valid object.
Can a null pointer be dereferenced in C?
No. Dereferencing a null pointer is invalid and leads to undefined behavior.
Why do we use NULL in C?
NULL is used to mark that a pointer intentionally points to no valid object, making the pointer state clear and easier to check.
What is the difference between null pointer and wild pointer?
A null pointer is intentionally empty, while a wild pointer is uninitialized and contains an unpredictable address.
What is the difference between null pointer and dangling pointer?
A null pointer points to nothing valid intentionally, while a dangling pointer points to memory that used to be valid but is no longer safe to use.
Why should pointers be initialized to NULL in C?
Initializing pointers to NULL helps avoid accidental use of garbage addresses and makes pointer state explicit.