C++ array: declare, initialize, passing array to function, pointer |OOP IN C++|

Write a C++ program that declares five integer variables a, b, c, d and e. Also an array of pointers is declared with five elements. Each element points to one of the integers. The program should input the five values in variable a, b, c, d and e. Display the values of pointer array as well as the address of each element/variable stored in pointer array.

Source Code:

#include<iostream>

using namespace std;

int main()

{

int a,b,c,d,e;

int *ptr[5];

ptr[0]=&a;

ptr[1]=&b;

ptr[2]=&c;

ptr[3]=&d;

ptr[4]=&e;

cout<<"Enter the five values:"<<endl;

cin>>a>>b>>c>>d>>e;

cout<<"The five values are:"<<endl;

cout<<"1st value: "<<*ptr[0]<<endl;

cout<<"2nd value: "<<*ptr[1]<<endl;

cout<<"3rd value: "<<*ptr[2]<<endl;

cout<<"4th value: "<<*ptr[3]<<endl;

cout<<"5th value: "<<*ptr[4]<<endl;

cout<<"The address of each element/variable of five values:"<<endl;

cout<<"1st value address: "<<ptr[0]<<endl;

cout<<"2nd value address: "<<ptr[1]<<endl;

cout<<"3rd value address: "<<ptr[2]<<endl;

cout<<"4th value address: "<<ptr[3]<<endl;

cout<<"5th value address: "<<ptr[4]<<endl;

 

return 0;

}







output:


Post a Comment

0 Comments