Open In App

NULL Pointer in C

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The Null Pointer is the pointer that does not point to any location but NULL. According to C11 standard:

“An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.”

Syntax of Null Pointer Declaration in C

type pointer_name = NULL;
type pointer_name = 0;

We just have to assign the NULL value. Strictly speaking, NULL expands to an implementation-defined null pointer constant which is defined in many header files such as “stdio.h”, “stddef.h”, “stdlib.h” etc.

Uses of NULL Pointer in C

Following are some most common uses of the NULL pointer in C:

  1. To initialize a pointer variable when that pointer variable hasn’t been assigned any valid memory address yet.
  2. To check for a null pointer before accessing any pointer variable. By doing so, we can perform error handling in pointer-related code, e.g., dereference a pointer variable only if it’s not NULL.
  3. To pass a null pointer to a function argument when we don’t want to pass any valid memory address.
  4. A NULL pointer is used in data structures like trees, linked lists, etc. to indicate the end.

Check if the pointer is NULL

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:

ptr == NULL;

The above equation will be true if the pointer is NULL, otherwise, it will be false.

Examples of NULL Pointer in C

Example 1: C Program to avoid segmentation fault while accessing the value of pointer using NULL pointer

C




// C NULL pointer demonstration
#include <stdio.h>
  
int main()
{
    // declaring null pointer
    int* ptr = NULL;
  
    // derefencing only if the pointer have any value
    if (ptr == NULL) {
        printf("Pointer does not point to anything");
    }
    else {
        printf("Value pointed by pointer: %d", *ptr);
    }
    return 0;
}


Output

Pointer does not point to anything

By specifically mentioning the NULL pointer, the C standard gives a mechanism using which a C programmer can check whether a given pointer is legitimate or not.

Example 2: C Program to check successful memory allocation using malloc()

The malloc() function returns the NULL pointer when the memory allocation is failed. We will use this property to check if the memory allocation is successful.

C




// C Program to use NULL pointer to check for melloc error
#include <stdio.h>
#include <stdlib.h>
  
int main()
{
  
    // declaring dynamic memory for ptr
    int* ptr = (int*)malloc(5 * sizeof(int));
  
    // checking if the memory allocation is successful
    if (!ptr) {
        printf("Memory Allocation Failed");
        exit(0);
    }
  
    return 0;
}


Example 3: Dereferencing a NULL Pointer in C

What’s going to happen if we use the following C code:

C




// Trying to dereferencing a null pointer
#include<stdio.h>
int main()
{
int * ptr = NULL;
printf("%d",*ptr);
return 0;
}


On some machines, the above would compile successfully but crash when the program is run. Though that doesn’t mean it would show the same behavior across all the machines. As the value of NULL in predefined libraries is 0 and the pointer (that’s pointing to NULL) is not pointing to any memory location, this behavior occurs.

Example 4: C Program to demonstrate how to pass NULL to a function

We can pass a NULL value for pointers that should not contain any value to the function in C.

C




// C Program to illustrate how passing NULL works
#include <stdio.h>
  
void foo(int* string)
{
    if (string == NULL) {
        printf("NULL Pointer Passed");
        return;
    }
    printf("Non-Null Pointer Passed");
}
  
int main()
{
  
    foo(NULL);
    return 0;
}


Output

NULL Pointer Passed

Difference Between NULL Pointer and Void Pointer in C

The following table list the differences between a null pointer and a void pointer in C:

NULL Pointer

Void Pointer

A NULL pointer does not point to anything. It is a special reserved value for pointers. A void pointer points to the memory location that may contain typeless data.
Any pointer type can be assigned NULL. It can only be of type void.
All the NULL pointers are equal. Void pointers can be different.
NULL Pointer is a value. A void pointer is a type.
Example: int *ptr = NULL; Example: void *ptr;


Last Updated : 10 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads