Thursday, 21 September 2017

Array of pointers in C++

/*
 Array of pointer in c++
*/

#include "iostream"
using namespace std;
const int MAX=3;

int main(){
 int var[MAX] = {10,100,200};
 int *ptr[MAX];
 cout<"\nPrinting array elements";
 for(int i=0; i<MAX; i++){
  cout<<var[i]<<endl;
 }
 
 cout<<"\nPrinting pointer elements"<<endl;
 for(int i=0; i<MAX; i++){
  ptr[i] = &var[i];
  cout<<*ptr[i]<<endl;
 }
 
 return 0;
}
/*
 OUTPUT
[pavan@localhost Pointers]$ g++ PointerArray.cpp
[pavan@localhost Pointers]$ ./a.out 
10
100
200

Printing pointer elements
10
100
200
[pavan@localhost Pointers]$ 

*/

No comments: