Thursday, 21 September 2017

Using array and pointers to print memory addresses in C++

/*
 Using Array and Pointer to print memory addresses in C++
*/

#include "iostream"
using namespace std;

int main(){
 int arr[5];
 int *ptr;
 
 
 cout<<"\nPrinting array addresses \n";
 for(int i=0; i<5; i++){
  cout<<"\n"<<&arr[i];
 }
 
           ptr = arr;
 cout<<"\n\nPrinting addresses using pointers";
 for(int i=0; i<5; i++){
  cout<<"\n"<<ptr+i;
 }
 
 cout<<"\n\nPrinting addresses of array using pointer notation ";
 for(int i=0; i<5; i++){
  cout<<"\n"<<arr+i;
 }
 cout<<endl;
}
/*
 OUTPUT
[pavan@localhost PointerArray]$ g++ PointerArray1.cpp
[pavan@localhost PointerArray]$ ./a.out 

Printing array addresses 

0x7fff80eaefc0
0x7fff80eaefc4
0x7fff80eaefc8
0x7fff80eaefcc
0x7fff80eaefd0

Printing addresses using pointers
0x7fff80eaefc0
0x7fff80eaefc4
0x7fff80eaefc8
0x7fff80eaefcc
0x7fff80eaefd0

Printing addresses of array using pointer notation 
0x7fff80eaefc0
0x7fff80eaefc4
0x7fff80eaefc8
0x7fff80eaefcc
0x7fff80eaefd0
[pavan@localhost PointerArray]$ 

*/

No comments: