C++ Program to print array using pointer

Simple C++ program to print array using pointer: We know pointer store the address of the variable and to print the value of address that is stored into pointer variable we use * sign. Suppose pointer is int *ptr and a variable is int n=9. To store address of variable n, we will write ptr=&n. We can print the value of n by n and *ptr. *ptr=n=9.
 
/* C++ Program to print array using pointer */
#include<iostream>
using namespace std;
int main()
{
 int arr[100],n,*ptr;
 cout<<"Enter total for elements: ";
 cin>>n;
 cout<<"Enter "<<n<<" elements: "<<endl;
 for(int i=0;i<n;i++)
 {
  cin>>arr[i];
 }
 /* point pointer ptr to first array element */
 ptr=arr;
 cout<<"Array: "<<endl;
 
 for(int i=0;i<n;i++)
 {
  cout<<*(ptr+i)<<" ";
 }
 return 0;
}
/* End of the program */


Popular posts from this blog