C++ Pointers

Every C++ variable hold a specific address in the memory. In C++ it is possible to access the memory location of any variable by using & (address-operator) with the variable name. Here is an example to access memory address of a variable using & operators as follows:

#include<iostream.h>

#include<conio.h>

int main()

{

int a;

cout<<"\nAddress of variable a:"<<&a;

getch();

return 0;

}

The output of the above program is as follows:

Address of variable a: 8124532

The above example prints the address of integer type variable a. You can also access the memory address of any variable using pointer. Now we are going to tell you about the pointers.

What is Pointer ?

  • A pointer is a special type of variable which is used to store a memory address of another variable.
  • A pointer can be named or declared in the same way as any other variable but it is always denoted by '*' symbol.
  • The basic syntax of a pointer in C++ is as follows:
  • data-type * pointer_variablename;
  • Here are some examples of declaration of pointer as follows:
int *a; /*interger pointer that can hold the address of any integer value*/

float *f; /*float pointer that can hold the address of any float value*/

char *c; /*character pointer that can hold the address of any character value*/

How to use a Pointer ?

  • As we already discussed that we can access the memory address of any variable by using pointer other than this we can also access the value available at that memory address through pointer. This can be done using the *operator that returns the value present at particular memory address.
  • Here is an example which shows the use of pointer to access the address of a variable and also the value present at that memory address:
  • #include<iostream.h>

    #include<conio.h>

    int main()

    {

    int *a; /*declaration of pointer type variable*/

    int b=5;

    a=&b;/* Assigning the address of variable b in the pointer type variable a */

    /*accessing the memory address of variable b using pointer*/

    cout<<"\nAddress of variable b stored in pointer type variable a:"<<a);

    /*accessing the value stored in variable using pointer*/

    cout<<"\nvalue of variable b:"<<*a);

    getch();

    return 0;

    }
  • The output of the above program is as follows:

Output

Address of variable b stored in pointer type variable a:10485132

value of variable b:5

Arithmetic Operations with Pointers

  • You can also perform arithmetic operation with pointers.
  • You can perform increase, decrease, prefix, postfix operation with the help of pointers.
  • You can see the effect of these operation on the different- different type of variables as follows:
  • In the above table you can see on incrementing the integer pointer p the address is increased by 4 i.e 2004 from 2000 because integer requires 4 bytes in memory area.
  • In the same way character and float pointer requires 1 and 4 bytes respectively that is why on decrementing the character pointer c the address is decreased by one i.e 2000 from 2001and on incrementing the float pointer f the address is increased by 4 i.e 2008 from 2004.
  • Here is an example of performing increment operation on pointer variables as follows:
  • #include<iostream.h>

    #include<conio.h>

    int main()

    {

    int a, *i;

    float b, *j;

    char c, *k;

    i=&a;

    j=&b;

    k=&c;

    cout<<"\nAddress of a :"<<i;

    cout<<"\nAddress of b:" <<j;

    cout<<"\nAddress of c:" <<k;

    i++;

    j++;

    k++;

    cout<<"\nAfter increment in pointers:";

    cout<<"\nAddress of a:" <<i;

    cout<<"\nAddress of b:" <<j;

    cout<<"\nAddress of c:" <<k;

    getch();

    return 0;

    }
  • The output of the above program is as follows:
  • Address of a :6944896

    Address of b:6944872

    Address of c :6944851

    After increment in pointers:

    Address of a :6944900

    Address of b:6944876

    Address of c :6944852
  • Here is another example of performing decrement operation on pointer variables as follows:
  • #include<iostream.h>

    #include<conio.h>

    int main()

    {

    int a, *i;

    float b, *j;

    char c, *k;

    i=&a;

    j=&b;

    k=&c;

    cout<<"\nAddress of a :"<<i;

    cout<<"\nAddress of b:" <<j;

    cout<<"\nAddress of c:" <<k;

    i--;

    j--;

    k--;

    cout<<"\nAfter decrement in pointers:";

    cout<<"\nAddress of a:" <<i;

    cout<<"\nAddress of b:" <<j;

    cout<<"\nAddress of c:" <<k;

    getch();

    return 0;

    }
  • The output of the above program is as follows:
Address of a :6944900

Address of b:6944876

Address of c :6944852

After decrement in pointers:

Address of a :6944896

Address of b:6944872

Address of c :6944851

Pointer and Array

  • Array name itself works like the pointer. It points the address of the first element of an array.
  • As we know elements of array are stored in the contiguous memory locations so elements of array together with their address can be displayed by the array name itself.
  • Here we have an example-program which display the array element with their address using the array-name as a pointer:
  • #include<iostream.h>

    #include<conio.h>

    int main()

    {

    int a[5]={1,2,3,4,5},i=0;

    cout<<"\nArray-Element Value Address");

    for(i=0;i<5;i++)

    {

    cout<<"\na["<<i<<"] "<<*(a+i)<<" "(a+i);

    getch();

    return 0;

    }
  • The output of the above program is as follows:
    Array-Element Value Address

    a[0] 1 2000

    a[1] 2 2004

    a[2] 3 2008

    a[3] 4 2012

    a[4] 5 2016
  • In the above example variable 'i' acts as an element number and its value varies from 0 to 4. When it is added with an array name 'a' that is with the address of first element of array then it points to consecutive memory locations so the program displays all the Array-element, their values and their addresses.

Array of Pointers

  • We can also create an array of pointer in C++. Array of pointers are collection of addresses which is used to store address of variables for which we have to declare an array as a pointer.
  • We can declare an array of pointer as follows:
  • int *arr[3];
  • Here is an example of array of pointer as given below:
  • #include<iostream.h>

    #include<conio.h>

    int main()

    {

    int a[5]={1,2,3,4,5},i=0;

    int *arr[5];//array of pointer

    for(i=0;i<5;i++)

    {

    arr[i]=&a[i];

    }

    cout<<"Display elements of array using array of pointer:";

    for(i=0;i<5;i++)

    {

    cout<<"\na["<<i<<"]="<<*arr[i];

    }

    getch();

    return 0;

    }
  • The output of the above program is as follows:
Display elements of array using array of pointers:

a[0]=1

a[1]=2

a[2]=3

a[3]=4

a[4]=5

Pointers to Pointers

  • A pointer variable can also store the address of another pointer variable. So the pointer variable that contains the address of another pointer variable is known as pointer to pointer.
  • The pointer to pointer can be declared as follows:
  • int **p;
  • Here is an example which shows the concept of pointer to pointer:
  • #include<iostream.h>

    #include<conio.h>

    int main()

    {

    int a, *p1,**p2;

    a=10;

    p1=&a;

    p2=&p1;

    cout<<"Through *p1:\nValue of a="<<*p1<<"\nAddress of a="<<p1;

    cout<<"\nThrough **p2:\nValue of a="<<**p2<<"\nAddress of a="<<*p1);

    getch();

    return 0;

    }
  • The output of the above program is as follows:
Through *p1:

Value of a=10

Address of a=4009

Through **p2:

Value of a=10

Address of a=4009