Using Pointers to Access Array Elements in C++

Exercise:

Write a C++ program that can be created to allow the Array elements to be accessed by pointers.

Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
    int Data[5];      // max 5 values
    cout << "Insert values:\n";
    for(int x = 0; x < 5; ++x)
    cin >> Data[x];
    cout << "\nInserted values are:";
    for(int x = 0; x < 5; ++x)
    cout << endl << *(Data + x);
    return 0;
}
Click Here to View the Output:
Insert values:
2
4
6
1
4
Inserted values are:
2
4
6
1
4
Click Here to View the Explanation:
  • 5 values are entered by the user in the program. int Data [5] is created to save the added values. Its length is limited to 5. They pass into the for loop.
  • The for loop assesses all the elements of the array and they’re printed onto the screen.
%d bloggers like this: