The Wayback Machine - https://web.archive.org/web/20240721034350/https://www.geeksforgeeks.org/null-pointer-in-c/
Open In App

NULL Pointer in C

Last Updated : 10 Apr, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
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;


Previous Article
Next Article

Similar Reads

C - Pointer to Pointer (Double Pointer)
Prerequisite: Pointers in C The pointer to a pointer in C is used when we want to store the address of another pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to cha
5 min read
Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Pointer to an Array | Array Pointer
Prerequisite: Pointers Introduction  Consider the following program:  C/C++ Code #include&lt;stdio.h&gt; int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf(&quot;%p\n&quot;, ptr); return 0; } In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer that can po
9 min read
Passing NULL to printf in C
Consider the following C code snippet. char* p = NULL; printf("%s", p); What should be the output of the above program? The print expects a '\0' terminated array of characters (or string literal) whereas it receives a null pointer. Passing NULL to printf is undefined behavior. According to Section 7.1.4(of C99 or C11) : Use of library functions If
1 min read
Dangling, Void , Null and Wild Pointers in C
In C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deallocated memory), Void (pointing to some data locati
6 min read
How to declare a pointer to a function?
While a pointer to a variable or an object is used to access them indirectly, a pointer to a function is used to invoke a function indirectly. Well, we assume that you know what it means by a pointer in C. So how do we create a pointer to an integer in C? Huh..it is pretty simple... int *ptrInteger; /*We have put a * operator between int and ptrInt
2 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being: 1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2. the &amp; operator array is an alias for &amp;array[0
1 min read
C | Pointer Basics | Question 1
What is the output of following program? # include &lt;stdio.h&gt; void fun(int x) { x = 30; } int main() { int y = 20; fun(y); printf(&quot;%d&quot;, y); return 0; } (A) 30 (B) 20 (C) Compiler Error (D) Runtime Error Answer: (B) Explanation: Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using
1 min read
C | Pointer Basics | Question 2
Output of following program? # include &lt;stdio.h&gt; void fun(int *ptr) { *ptr = 30; } int main() { int y = 20; fun(&amp;y); printf(&quot;%d&quot;, y); return 0; } (A) 20 (B) 30 (C) Compiler Error (D) Runtime Error Answer: (B) Explanation: The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value
1 min read
C | Pointer Basics | Question 3
Output of following program? #include &lt;stdio.h&gt; int main() { int *ptr; int x; ptr = &amp;x; *ptr = 0; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr); *ptr += 5; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr); (*ptr)++; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr);
2 min read
Article Tags :
three90RightbarBannerImg