Array of Pointer in C++

array of pointer in C++ based on two different concept of C++, are follows

The collection of pointer is called array of pointer.

In other words, the array of pointer is the collection of address  which store the array of elements. Thus each elements of the array become pointer type elements.

 

array-of-pointer-in-cpp

while, access these address’s value, we use *ptr[0]*ptr[1],*ptr[2],*ptr[3] in same order.

let’s discuss more about this,

Declaration of array of pointer in C++

SYNTEX:

Data type *array_name[size];

Example:

int *ptr[6];

int arr[6] = {3,2,4,3,1,6};

As we know, we can access an array element from index value i.e.

cout<<*ptr[0] ; //3
cout<<*ptr[1]; //2
cout<<*ptr[2]; //4
cout<<*ptr[3]; //3
cout<<*ptr[4] ; //1
cout<<*ptr[5] ; //6

while accessing address, address of 3 in ptr[0]

ptr[0] = &arr[0]; address of 3 in ptr[0]
ptr[1] = &arr[1]; address of 2 in ptr[1]
ptr[2] = &arr[2]; address of 4 in ptr[2]
ptr[3] = &arr[3]; address of 3 in ptr[3]
ptr[4] = &arr[4]; address of 1 in ptr[4]
ptr[5] = &arr[5]; address of 6 in ptr[5]

will be the store.

You can understand this through the program given below-

#include<iostream.h>
#include<conio.h>

void main()
 {
    int *ptr[6];
    int i,arr[6] = {3,2,4,3,1,6};

    clrscr();

     for(i= 0; i<5; i++)
      {
        ptr[i] = &arr[i];     //assigning each element address in ptr[i]

        cout<<"address of value "<<arr[i]<<" is "<<ptr[i]<<endl;
      }

    cout<<"\nvalues of these address are:\n";

     for(i= 0; i<5; i++)
      { 
        cout<<*ptr[i]<<" ";          //displaying elements
      }
    getch();
 }
OUTPUT:-
address of value 3 is 0x8f2bff8e
address of value 2 is 0x8f2bff90
address of value 4 is 0x8f2bff92
address of value 3 is 0x8f2bff94
address of value 1 is 0x8f2bff96

values of these address are:
3 2 4 3 1

read this also


previous-Pointer in C++

next-Memory Management and Types