Thursday, 21 September 2017

Array of pointers in C++

/*
 Array of pointers in C++
*/
#include "iostream"
using namespace std;
const int MAX=3;

int main(){
 int var[MAX] = {10,20,30};
 int *ptr[MAX];
 
 cout<<"\nPrinting Array elements \n";
 for(int i=0; i<MAX; i++)
  cout<<" "<<var[i];
 cout<<endl;

 cout<<"\nAssigning address of array elements to pointer ";
 for(int i=0; i<MAX; i++)
  ptr[i] = &var[i];

 cout<<endl;
 cout<<"\nPrinting array elements address using pointers \n";
 for(int i=0; i<MAX; i++)
  cout<<" "<<ptr[i];
 cout<<endl;

 cout<<"\nPrinting array elements using pointers \n";
 for(int i=0; i<MAX; i++)
  cout<<" "<<*ptr[i];
 cout<<endl;
 return 0;
}
/*
 OUTPUT
[pavan@localhost Home]$ g++ ArrayOfPointer.cpp
[pavan@localhost Home]$ ./a.out 

Printing Array elements 
 10 20 30

Assigning address of array elements to pointer 

Printing array elements address using pointers 
 0x7ffc0e5246c0 0x7ffc0e5246c4 0x7ffc0e5246c8

Printing array elements using pointers 
 10 20 30
[pavan@localhost Home]$ 
*/

No comments: