Why is address zero used for the null pointer in C/C++?


Null pointer is a pointer which points nothing.

Some uses of null pointer are:

b) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.

b) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.

c) To check for 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.

In C++ if we assign 0 in any pointer that means the pointer pointing to the NULL.

Syntax

Float *p = 0 //initializing the pointer as 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:

 Live Demo

#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.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

464 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements