41.3k views
2 votes
Write a program that defines an array of integers and a pointer to an integer. Make the pointer point to the beginning of the array. Then, fill the array with values using the pointer (Note: Do NOT use the array name.). Enhance the program by prompting the user for the values, again using the pointer, not the array name. Use pointer subscript notation to reference the values in the array.

1 Answer

5 votes

Answer:

#include<iostream>

using namespace std;

int main()

{

int a[20],n;

int *p;

cout<<"\\ enter how many values do you want to enter:";

cin>>n;

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

{

cout<<"\\ Enter the "<<i+1 <<"th value :";

cin>>*(p+i);

//reading the values into the array using pointer

}

cout<<"\\ The values in the array using pointer is:\\";

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

{

cout<<"\\ (p+"<<i<<"): "<<*(p+i);

//here we can use both *(p+i) or p[i] both are pointer subscript notations

}

}

Output:

Write a program that defines an array of integers and a pointer to an integer. Make-example-1
User Mike Williams
by
3.5k points