array of structure in C++ program

an array of structure in C++ is a combination of two different concepts are following,

using the array of structure in C++, we can create all the elements of an array in a structure type element, which means that every element of the array will be a structure type elements. as we know structure can store different data type so in a simple way you can say using an array of structure in C++ we can store an array dissimilar data type indirectly.

You can understand this with the help of the diagram given below-

Here arr[4] is the array type variable of a structure.

as you can see above, the structure-variable name is same but due to different index value, structure-variable are different from each other.

In array of structure, we make structure-variable as an array variable. its syntax is given below-

declaration of a structure-variable

Here is the syntax, below

structure_name structure_variable[size];

In this we first declare structure members and then declare the array variable of a structure-variable such as,

struct student{
   int roll,age
   char name[10];
book vr[2];

In the above structure declaration, we have stored the record (roll, age and name) of two students using the array of structure.

For 1st student


For 2nd student,


as you can see in the below diagram also,

accessing member in an array of structure

Because here it contains the element of structure and as we know that we use index value to access the array element. So means, the structure will be accessed by index value just like a simple array variable. like-

structure_variable [value].structure_member_name

Let’s try with an example


using namespace std;
// struct declaration outside main() 
  int roll,age;
  char name[20];

int main()
  for(int i=0; i<2; i++)
     cout<"Enter "<<i+1<<" Student Record\n";
     cout<<"Enter Roll no: ";
     cout<<"Enter Name : ";
     cout<<"Enter age : ";
  for(int j=0; j<2; j++)
    cout<<"\nDisplay "<<j+1<<" Student Record";
    cout<<"\nRoll no.: "<<vr[j].roll;
    cout<<"\nName : "<<vr[j].name;
    cout<<"\nAge : "<<vr[j].age;
return 0;


Enter 1 Student Record
Enter Roll no: 10
Enter Name : Rahul sherma
Enter age : 25
Enter 2 Student Record
Enter Roll no: 11
Enter Name : Rohit kumar
Enter age : 24

Display 1 Student Record
Roll no.: 10
Name : Rahul sherma
Age : 25
Display 2 Student Record
Roll no.: 11
Name : Rohit kumar
Age : 24

Because class and structure correspond to each other in many cases. Therefore, the array-of-structure and array-of-object are almost the same.

Related exercise

Like it?

Leave a Reply

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

Exit mobile version