CPP program Accessing the element of array by using pointer

 Here, in this example of the cpp program declare an array of five lengths with integer type and take input from the user then access all the elements of the array through a pointer.


For understanding you have knowledge about the following topics:

C++ pointers

C++ Arrays


Code:


#include <iostream>

using namespace std;


int main()

{

   int arr[5];

   cout << "Enter elements :- ";


   for(int i = 0; i < 5; ++i)

   {

      cin >> arr[i];

  }


   cout << "You entered arr :- "<<endl;;

   for(int i = 0; i < 5; ++i)

   {


      cout <<  *(arr + i)<<endl;

  }


   return 0;

}


Output:

Enter elements:- 1

2

3

4

5

You entered arr:-

1

2

3

4

5



Comments

Popular posts from this blog

how to cout in cpp

Flutter layout

input by cin in c++