25. C – Null Pointer

By | November 11, 2020

A null pointer is a pointer which points nothing.

Some uses of the null pointer are:

  • To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.
  • To pass a null pointer to a function argument when we don’t want to pass any valid memory address.
  • To check for a null pointer before accessing any pointer variable. So that, we can perform error handling in pointer-related code e.g. dereference pointer variable only if it’s not NULL.

Algorithm

Begin.
   Declare a pointer p of the integer datatype.
      Initialize *p= NULL.
   Print “The value of pointer is”.
      Print the value of the pointer p.
End.

Example

#include <stdio.h>
int main() {
   int *p= NULL;//initialize the pointer as null.
   printf("The value of pointer is %u",p);
   return 0;
}

Output

The value of pointer is 0.

Leave a Reply

Your email address will not be published. Required fields are marked *