Null pointer

Null pointer is a pointer they point nothing in memory location and the null pointer is basically stored null value while void is the type of the pointer. It can be stores the base address of the segment.null means the pointer refer to the 0th memory location. We do not have address which is assigned to the pointer it is known as null pointer.

Following applications are null pointer:

  • It is used to perform error handling with pointer before de-referencing the pointers.
  • It is used to initalize 0 pointer variable when the pointer does not point to valid memory address.
  • Pass a null pointer to a function argument we do not want to pass the actual memory address.

Examples of the 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 see the example of malloc()function in null pointer

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

Output

Memory is allocated

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews