Null Pointer In C/C++

Share Your Love

A Null Pointer is a pointer that doesn’t point to any memory location. It saves the segment’s base address. The null pointer is used to hold null values, and the type of the pointer is void.

A null pointer is a special reserved value declared in a header file called stddef. Null indicates that the pointer is pointing to the first memory location.

A null pointer is created when there is no address to be allocated to the pointer. When a NULL value is assigned to a pointer, the pointer is considered null.

Applications of Null Pointer:

A Null pointer can be used in the following ways:

  • When a pointer variable does not point to a valid memory address, it is used to initialise it.
  • Before dereferencing pointers, it is utilised to do error handling with them.
  • When we don’t want to supply the actual memory address, it’s passed as a function argument and used to return from a function.

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 take a look at when we might need to use the null pointer.

  • When the pointer variable has no memory address assigned to it.
#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;  
}  

We declare the pointer variable *ptr in the preceding code, but it does not contain the address of any variable. Because it does not point to any variable, dereferencing the uninitialized pointer variable will result in a compile-time error.

The local variables of a function are stored in the stack according to the stack memory idea, and if the variable has no value, it displays the garbage value. The above software produces various unexpected outcomes and eventually crashes. As a result, we can conclude that leaving an uninitialized pointer in a program can cause serious computer damage.

How to avoid the above situation?

Using the Null pointer, we may avoid the issue described above. A null pointer points to the 0th memory location, which is a reserved memory region that cannot be dereferenced.

#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 establish a pointer *ptr and give it a NULL value, indicating that it does not point to any variable. Following the creation of a pointer variable, we add a condition that checks whether the value of the pointer is null.

When we use the malloc() function?

#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;  
}  

We use the library function, malloc(), in the above code. As we know, the malloc() method allocates memory; if the malloc() function is unable to allocate memory, the NULL pointer is returned. As a result, a condition must be included to verify whether the value of a pointer is null or not; if the value of a pointer is not null, the memory is allocated.

Output:

Memory is allocated.

When we don’t know the actual address of memory, it’s always a good idea to set a Null value to the pointer.

Note

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter