C++ Program to Access Elements of an Array Using Pointer

#include <iostream>
using namespace std;
int main()
{
   int a[100],n,i;
   cout<<"Enter size of Array :: ";
    cin>>n;
    cout<<"\nEnter elements to the array :: \n";
    for(i=0;i<n;++i)
    {
        cout<<"\nEnter "<<i+1<<" element :: ";
        cin>>a[i];
    }
   cout << "Accessing Elements through Pointers are :: \n";
   for(int i = 0; i < n; ++i)
   {
       cout << endl << *(a + i);
   }
    cout<<"\n";
   return 0;
}

OUTPUT:

Enter size of Array :: 6
Enter elements to the array ::
Enter 1 element :: 1
Enter 2 element :: 2
Enter 3 element :: 3
Enter 4 element :: 4
Enter 5 element :: 5
Enter 6 element :: 6
Accessing Elements through Pointers are ::
1
2
3
4
5
6

Sharing Is Caring

Leave a Comment