Signup/Sign In

C++ Program To Print Address Of Pointer Of An Array Using Pointer

In this tutorial, we will learn how to print the address of the pointer of an array using a pointer.

Print Address Of Pointer Of An Array In C++

Before proceeding to the implementation of the program, let's understand the approach.

Here, for the address of array element in c++, we have to print the address of an array(an array each variable of an array) or we have to display the memory location of each element of an array we can do this by adding "address of" or "&" operator. Ths "&" operator returns the address of a variable in a memory location. or we can do this by using a pointer for that we have to transfer all array elements to pointer one by one and print the pointer value as we know that pointer is a variable that holds the address of another variable so each time in a Loop we assign the array value to a pointer and print the value of hold by the pointer.

#include<bits/stdc++.h>
using namespace std;

int main()
{
 
 int i, size;
 
 cout<<"=======================================\n";
 cout<<"Enter The Size of The Array ";
 cout<<"\n=======================================\n";
 
 cin>>size;
 
    int array[size];
    int *ptr;
 
 cout<<"=======================================\n";
 cout << "Enter The Elements Of An array";
 cout<<"\n=======================================\n";
 
 for (i = 0; i < size; i++) 
 {
        cin>>array[i];    
 }
 
 cout<<"=======================================\n";
 cout << "Displaying An Address Using An Arrays";    
 cout<<"\n=======================================\n\n";
 for (i = 0; i < size; i++) 
 {
        cout << "Address Of " << array[i]<<" Using Array is ===> " << &array[i]<<endl;    
 }

    cout<<"\n=======================================\n";
 cout << "Displaying An Address Using Pointers";
 cout<<"\n=======================================\n";
    for (i = 0; i < size; i++) 
 {
  ptr = &array[i];   // ptr = &a[0]
        cout << "Address Of " << array[i] << " Using Pointers is ===> "<<ptr<<endl;
 }
 cout<<"\n========================================\n";
    return 0;
}


=======================================
Enter The Size of The Array
=======================================
5
=======================================
Enter The Elements Of An array
=======================================
2 3 6 1 9
=======================================
Displaying An Address Using An Arrays
=======================================

Address Of 2 Using Array is ===> 0x7ffca1328530
Address Of 3 Using Array is ===> 0x7ffca1328534
Address Of 6 Using Array is ===> 0x7ffca1328538
Address Of 1 Using Array is ===> 0x7ffca132853c
Address Of 9 Using Array is ===> 0x7ffca1328540

=======================================
Displaying An Address Using Pointers
=======================================
Address Of 2 Using Pointers is ===> 0x7ffca1328530
Address Of 3 Using Pointers is ===> 0x7ffca1328534
Address Of 6 Using Pointers is ===> 0x7ffca1328538
Address Of 1 Using Pointers is ===> 0x7ffca132853c
Address Of 9 Using Pointers is ===> 0x7ffca1328540

========================================

Conclusion

Here, we have learned how to implement a C++ program For Printing the Address Of Pointer Of An Array Using Pointer.