More about Pointers in C++

Written by

Vaaruni Agarwal

So far we have read about, a very important concept in C++, that is the pointers. But, there is so much more to the topic that we will focus on now.


Null Pointer:

A pointer that is assigned NULL is called a null pointer. This is done at the time of variable declaration. The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Here is an example illustrating the use of the NULL pointer in C++:

#include <iostream.h>

void main () {

   int  *ptr = NULL;

   cout << "The value of ptr is " << ptr ;



The value of ptr is 0

The memory address 0 signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.

To check for a null pointer you can use an if statement as follows −

if(ptr)     // succeeds if p is not null

if(!ptr)    // succeeds if p is null

The significance of the NULL pointer lies in the fact that if we accidentally try to access an uninitialized value, then it may cause abnormal program termination, however, if the pointers are initialized as NULL beforehand then any such case can be prevented easily.


Pointers to Arrays:

A pointer that points to the beginning of an array can access every element of the array by using array-style indexing.


void main () {

   int  var[MAX] = {10, 100, 200};

   int  *ptr;

    // let us have array address in pointer.

   ptr = var;

      for (int i = 0; i < MAX; i++) {

             cout << "Value of var[" << i << "] = ";

      cout << *ptr << endl;

       // point to the next location




More about Pointers in C++