Loading....
Coupon Accepted Successfully!

 

Arrays Of Pointers

So far, we have studied arrays of different standard data types, such as arrays of intfloat, and char. In the same way, the ‘C++’ language also supports arrays of pointers. It is nothing but a collection of addresses. Here, we store the addresses of variables for which we have to declare arrays as pointers.
 

12.6 Write a program to store addresses of different elements of an array using array of pointers.

#include<conio.h>

#include<iostream.h>

int main()

{

int *arrp[3];

int arr[3]={5,10,15},k;

for(k=0;k<3;k++)

arrp[k]=arr+k;

clrscr();

cout<<“\n\t Address Element”<<endl;

for (k=0;k<3;k++)

{

cout<<“\t” <<unsigned(arrp[k]);

cout<<“\t”<<*(arrp[k])<<endl;

}

return 0;
}

OUTPUT
Address Element
65418 5
65420 10
65422 15

Explanation: In the above program, *arrp[3] is declared as an array of pointers. Using first for loop, the addresses of various elements of array ‘arr[]’ are assigned to *arrp[]’. The second for loop picks up addresses from *arrp[]’ and displays the values present at those locations. Here, each element of *arrp[]’ points to the respective element of array ‘arr[]’.

Table: Arrays of pointers in memory
 

Element no.

Array of values

Element no.

Array of addresses

arr[0]

5

arrp[0]

65418

arr[1]

10

arrp[1]

65420

arr[2]

15

arrp[2]

65422





Test Your Skills Now!
Take a Quiz now
Reviewer Name