Address of an Array C++: We can do this by using a pointer for that we have to transfer all array elements to the pointer one by one and print the pointer value as we know a 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. How to print array in C++.
Program to Print the Address of The Pointer of an Array In C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
/*Print the Address of The Pointer of an Array In C++*/
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 the Address of The Pointer of an Array";
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 the Address of The Pointer of an Array";
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;
}
0 Comments: