What is NULL pointer in C

What is NULL pointer in C

NULL pointer in C is a pointer which is pointing to nothing. It is used to initialize a pointer at the time of declaration if we don’t have any explicit value to initialize. It is a good practice to initialize a pointer with NULL to ensure that it is not pointing to a random memory location. The NULL is a macro constant with a value of zero defined in various C header files like stdio.h, stdlib.h, alloc.h etc.
A pointer initialized with NULL is known as NULL pointer.

int *ptr = NULL;

Pointer ptr is initialized with NULL. Pointer ptr is not pointing to any valid memory location. We can check whether a pointer is a NULL pointer or not as follows:

if(ptr == NULL)
if(!ptr)

What are the advantages of using Pointers in C

  • We can dynamically allocate or deallocate space in memory at run time by using pointers.
  • Using pointers we can return multiple values from a function.
  • We can pass arrays to a function as call by Reference.
  • Pointers are used to efficiently access array elements, as array elements are stored in adjacent memory locations. If we have a pointer pointing to a particular element of array, then we can get the address of next element by simply incrementing the pointer.
  • Pointers are used to efficiently implement dynamic Data Structures like Queues, Stacks, Linked Lists, Tress etc.
  • The use of pointers results into faster execution of program.