Pointer to an Array in C++

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

//Pointers with Array
#include<iostream>
using namespace std;
int main()
{
    system("cls");
   int ar[]={10,20,30,50,60};
   //             0  1   2   3  4 
   int *p,sum=0;
   p=&ar[4];
   for(int i=100;i<105;i++)
   {
    cout<<endl<<*p;
    sum=sum+*p;
    --p;
   }
   cout<<"\nsum="<<endl<<sum;
    
       return 0;
}   
//int *p;
//    p=&ar[0];   
//    cout<<endl<<*p;
//    ++p;  //address    
//    cout<<endl<<*p;
//    ++p;
//    cout<<endl<<*p;

Program 2

#include<iostream>
using namespace std;
int main()
{
    system("cls");
    int a=50,b=20,*p;
    p=&a;
    cout<<*p;  //50
    ++p;   
    cout<<endl<<*p;  
    //     int a,b,c,d,e;
    // cout<<endl<<&a;
    // cout<<endl<<&b;
    // cout<<endl<<&c;
    // cout<<endl<<&d;
    // cout<<endl<<&e;

    return 0;
}

Program 3

//Pointers with Array
#include<iostream>
using namespace std;
int main()
{
    system("cls");
    int a[100],n;
    cout<<"Enter the size of array\n";
    cin>>n;
    cout<<"Enter elements in array\n";
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int *p;
    p=&a[0];
    for(int i=0;i<n;i++)
    {
           cout<<endl<<*p;
           ++p;
    }
}

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *