C++ Structure Array

The arrays and structures can be combined to form complex data objects. There may be structures contained within an array; also, there may be an array as an element of a structure. Let's discuss various combinations of arrays and structures.

Arrays of structures

Since an array can contain similar elements, the combination of structures within an array is an array of structures. To declare an array of structures, you must first define a structure and then declare an array variable of that type. For example, to store the addresses of 100 members of the council, you need to create an array.

Now, to declare a 100-element array of structures of type addr (defined in previous articles), we will write:

addr mem_addr[100];

This creates 100 sets of variables that are organized as defined in the structure addr. To access a specific structure, index the structure name. For instance, to print the houseno of structure 8, write:

cout<<mem_add[7].houseno;

Always remember that for all arrays in C++, indexing begins at 0.

An array of structures may even contain nested structures. For example, you can even create an array having structures of type "emp," which is a nested structure (already defined in the previous post):

emp sales_emp[100];

The above declaration creates an array sales_emp to store 100 structures of the emp type.

C++ Structure Array Example

Here is an example program demonstrating a structure array (an array of structures) in C++. This program creates an array of structures, reads information into it, and displays the information of employees depending upon the entered data.

#include<iostream>
using namespace std;

struct emp      // structure name (emp)
{
   string name;      // structure member
};

int main()
{
   emp e[5];      // array-of-structure variable (e[5])
   int i;

   for(i=0; i<5; i++)
   {
      cout<<"Enter the name of the employee no."<<i+1<<": ";
      cin>>e[i].name;
   }

   cout<<"\nNames of all employees: \n";
   for(i=0; i<5; i++)
   {
      cout<<e[i].name<<endl;
   }
   cout<<endl;

   return 0;
}

The following snapshot shows the initial output produced by the above program:

c++ structure array

Now enter employee No. 1, say "William," and press the ENTER key. Then type the second employee's name, say "Edwin," and press the ENTER key again, and so on. In this manner, enter five names and press the ENTER key at the end to get the following output:

c++ array of structure

Let me go over all of the steps involved in the preceding program.

Let me create one more example that uses multiple structure members. This program illustrates a structure array. In this program, I will not use the named structure. Therefore, I will declare the array-of-structure variable at the time of declaring the structure.

#include<iostream>
using namespace std;

struct {
   int sno;
   long int empId;
   string empName;
   string empCity;
} a[2];

int main()
{
   cout<<"----Enter the data for the first employee----\n";
   cout<<"Enter the Serial Number: ";
   cin>>a[0].sno;
   cout<<"Enter the ID: ";
   cin>>a[0].empId;
   cout<<"Enter the Name: ";
   cin>>a[0].empName;
   cout<<"Enter the City: ";
   cin>>a[0].empCity;

   cout<<"\n\n----Enter the data for the second employee----\n";
   cout<<"Enter the Serial Number: ";
   cin>>a[1].sno;
   cout<<"Enter the ID: ";
   cin>>a[1].empId;
   cout<<"Enter the Name: ";
   cin>>a[1].empName;
   cout<<"Enter the City: ";
   cin>>a[1].empCity;

   cout<<"\n\nData\t\tEmployee 1\t\tEmployee 2";
   cout<<"\nS.No.\t\t"<<a[0].sno<<"\t\t\t"<<a[1].sno;
   cout<<"\nID\t\t"<<a[0].empId<<"\t\t\t"<<a[1].empId;
   cout<<"\nName\t\t"<<a[0].empName<<"\t\t\t"<<a[1].empName;
   cout<<"\nCity\t\t"<<a[0].empCity<<"\t\t\t"<<a[1].empCity;

   cout<<endl;
   return 0;
}

The screenshots below depict a sample run of this C++ example program demonstrating the structure array in C++.

c++ structure array example program

Let me now create another example in C++ that makes use of the structure array. This program allows the user to enter the information for three employees and then enter the employee ID to retrieve and display the information for the employee with the given ID number. I kept the structure array size to 3 to keep the output console short and visible with a single snapshot of the output console's sample run.

#include<iostream>
using namespace std;

struct emp
{
   int id ;
   string name;
   long int houseno;
   string area;
   string city;
   string state;
};

int main()
{
   emp e[3];

   int i, temp=0;
   long int x;

   for(i=0; i<3; i++)
   {
      cout<<"----Enter the data for the employee no."<<i+1<<"----\n";
      cout<<"Enter the ID: ";
      cin>>e[i].id;
      cout<<"Enter the Name: ";
      cin>>e[i].name;
      cout<<"Enter the House No.: ";
      cin>>e[i].houseno;
      cout<<"Enter the Area: ";
      cin>>e[i].area;
      cout<<"Enter the City: ";
      cin>>e[i].city;
      cout<<"Enter the State: ";
      cin>>e[i].state;
   }

   cout<<"\nEnter the employee ID to display the details: ";
   cin>>x;
   for(i=0; i<3; i++)
   {
      if(x == e[i].id)
      {
         cout<<"\nName: "<<e[i].name;
         cout<<"\nAddress: "<<e[i].houseno<<", "<<e[i].area<<", "<<e[i].city<<", "<<e[i].state;
         temp = 1;
         break;
      }
   }

   if(temp == 0)
      cout<<"\nThe employee identified by the given ID does not exist.";

   cout<<endl;
   return 0;
}

The following snapshot shows a sample run of the above program with the following user inputs:

That is, I typed 1006 and hit the ENTER key, then typed "William" and hit the ENTER key, and so on. And then I typed 1007 as the ID of the employee whose details I needed to be displayed on the output console.

array of structure in C++ example program

C++ arrays within structures

As already mentioned, a structure element may be either simple or complex. A complex structure may itself be a structure or an array. When a structure element happens to be an array, it is treated in the same way as arrays are treated. The only traditional thing to keep in mind is that, to access it, its structure name followed by a dot (.) and the array name are to be given. For example, consider this structure:

struct student
{
   int rollno;
   char name[21];
   float marks[5];      // Array "marks" is now a member element of structure "student."
};
student learner;

The above-mentioned structure variable "learner" is of structure type "student" and contains an element that is an array of 5 floats used to store a student's marks in five different subjects. To reference marks for the structure learner's third subject, we will write:

learner.marks[2];

The array in a structure may even be two-dimensional, as shown below:

struct type
{
   int x[5][5];      // 5 × 5 array of int
   float y;
}svar;

C++ Quiz


« Previous Tutorial Next Tutorial »


Liked this post? Share it!