C Null Pointers with examples

Simple Pointer Definition

Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time.

By using pointers we can get multiple values from the function, as we know a function can return only one value but by passing arguments as pointers we can get more than one values from the pointer.

C Null Pointer Definition

The word "NULL" is a constant in C language and its value is 0, it means a null pointer is a pointer that does not point to any object or function is called NULL pointer.

Pointers declared in program but not initialized contain garbage addresses, pointing to locations beyond the storage allocated to program by the compiler.

Example 1:



Example 2:


In exp. "int *nptr = 0"
nptr is a NULL pointer!
In exp. "char *cp = 0"
cp is a NULL pointer!
In exp. "float *fp = 0"
fp is a NULL pointer!

Example 3:


 Default Address "iptr" holds as (nil)
 Default Address "ip" holds as 0x7fff69f3bd80
 Default Address "cp" holds as 0x400420
 Default Address "fp" holds as (nil)