C Null Pointer

A pointer that is not assigned any value but NULL is known as the NULL pointer. A Null Pointer is a pointer that does not point to any memory location. If you don't have any address to be specified in the pointer at the time of declaration, you can assign NULL value.

To define a null pointer assign the predefined constant NULL which is defined in standard(stddef) header files, including <stdio.h>, <stdlib.h>, and <string.h>. Here, Null means that the pointer is referring to the 0th memory location. In the most libraries, the value of the pointer is 0 (zero).

snippet
#include 
int *ip = NULL;

To test it for a null pointer before inspecting the value pointed to you might use code like below.

snippet
if(ip != NULL)
 printf("%d\n", *ip);

Usage of Null Pointer

Following are the uses of a Null pointer:

  • It is used to initialize 0 pointer variable when the pointer does not point to a valid memory address.
  • It is used to perform error handling with pointers before dereferencing the pointers.
  • It is passed as a function argument and to return from a function when we do not want to pass the actual memory address.

We can also refer to the null pointer by using a constant 0, and you will see some code that sets null pointers by simply doing as beow

int *ip = 0;

As the definition of true in C is a value that is not equal to 0, you can tests for non-null pointers with abbreviated code like below.

snippet
if(ip)
printf("%d\n", *ip);

It has the same meaning as our previous example; if(ip) is equivalent to if(ip != 0) and to if(ip != NULL).

Examples of Null Pointer

int *ptr=(int *)0;
float *ptr=(float *)0;
char *ptr=(char *)0;
double *ptr=(double *)0;
char *ptr='\0';
int *ptr=NULL;

Let's look at the situations where we need to use the null pointer.

When we do not assign any memory address to the pointer variable.

snippet
#include <stdio.h>
int main()
{
    int *ptr;
   printf("Address: %d", ptr); // printing the value of ptr.
   printf("Value: %d", *ptr); // dereferencing the illegal pointer
   return 0;
}

In the above code, we declare the pointer variable *ptr, but it does not contain the address of any variable. The dereferencing of the uninitialized pointer variable will show the compile-time error as it does not point any variable. According to the stack memory concept, the local variables of a function are stored in the stack, and if the variable does not contain any value, then it shows the garbage value. The above program shows some unpredictable results and causes the program to crash. Therefore, we can say that keeping an uninitialized pointer in a program can cause serious harm to the computer.

Avoiding Pointer Errors

Using Null Pointer

We can avoid the error created in the previous example by using the Null pointer. A null pointer is a pointer pointing to the 0th memory location, which is a reserved memory and cannot be dereferenced.

snippet
#include <stdio.h>
int main()
{
    int *ptr=NULL;
    if(ptr!=NULL)
    {
        printf("value of ptr is : %d",*ptr);
    }
    else
    {
        printf("Invalid pointer");
    }
  return 0;
}

In the above code, we create a pointer *ptr and assigns a NULL value to the pointer, which means that it does not point any variable. After creating a pointer variable, we add the condition in which we check whether the value of a pointer is null or not.

Using the malloc() function

snippet
#include <stdio.h>
int main()
{
    int *ptr;
    ptr=(int*)malloc(4*sizeof(int));
    if(ptr==NULL)
    {
        printf("Memory is not allocated");
    }
    else
    {
        printf("Memory is allocated");
    }
    return 0;
}

In the above code, we use the library function, i.e., malloc(). As we know, that malloc() function allocates the memory; if malloc() function is not able to allocate the memory, then it returns the NULL pointer. Therefore, it is necessary to add the condition which will check whether the value of a pointer is null or not, if the value of a pointer is not null means that the memory is allocated.

Null Pointer in C
Note
It is always a good practice that we assign a Null value to the pointer when we do not know the exact address of memory.
Points to Remember
  1. Always initialize pointer variables as NULL.
  2. Always perform a NULL check before accessing any pointer.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +