by Dinesh Thakur Category: Array Pointer and Union

An array of structures refers to an array in which each element is of structure type. To declare an array of structures, firstly, a structure is defined and then an array of that structure is declared. The syntax for declaring an array of structures is

         structure_name structure_variable[size];

To understand the concept of declaring an array of structures, consider this example.

Example : A code segment to declare an array of structures

struct sales_record

{

//same as

} ;

sales_record item[2]; //an array of structures

 

In this example, an array item of type structure sales_record is declared. That is, the array elements item [0] and item [1] are structure variables of structure type sales_record. In addition, the compiler allocates 18 bytes of memory space (9 bytes for each element) to item and the array elements item [0]and item [1]are stored in adjacent memory locations.

Now, the array elements can be initialized using this statement.

sales_record item[]={ {'A',151,423,86.53}, {'C',152,654,90.38} };

In this statement, the elements of array item are initialized with specific values and the size of array is automatically set to two.

Once an array of structures is declared and initialized, the members of the array (that is, structure variables) can be accessed using the dot operator. The syntax for accessing members of an array of structures is

           structure_variable[index].member_name

where, index can be any integer variable or integer constant or an expression evaluated to integer value.

For example, the array elements of item can be accessed using these statements.

Cout<<item[0].grade; //display grade of 1st array element

Cout<<item[1].item_rate; //display item_rate of IInd

                         //array element

To understand the concept of array of structures, consider this example.

Example: A program to illustrate the usage of array of structures

#include<iostream>

using namespace std;

const int size=2; //size of array

struct employee //structure definition

{

int emp_no;

char name [10];

float salary;

} ;

int main ()

{

//array within structure

employee emp[size]; //declaring array of structures emp

for(int i=0;i<size;i++) //reading data into emp

cout<<"Enter' details for employee #"<< (i+l)<<": \n";

cout<<" Code: ";

cin>>emp[i].emp_no; //accessing members

cout<<" Name:”;

cin>>emp[i]name;

cout<<" Salary:";

cin>>emp[i] .salary;

cout<<"\n" ;

for(int i=0;i<size;i++) //displaying data of emp

{

Cout<<"Details of employee #"<<(i+1)<<" are:\n";

Cout<<" Code: "<<emp[i] .emp_no<<"\n" ;

Cout<<" Name: "<<emp[i] .name<<"\n";

Cout<<" Salary: "<<emp[i] .salary<<"\n";

}

return 0;

}

The output of the program is

Enter details for employee #1:

Code: 101

Name: Smith

Salary: 9545.50

Enter details for employee #2:

Code: 102

Name: Robert

Salary: 8567.35

Details of employee #1 are:

Code: 101

Name: Smith

Salary: 9545.5

Details of employee #2 are:

Code: 102

Name: Robert

Salary: 8567.35

In this example, an array emp of structure type employee is declared. The values of members of emp are accepted from the user and are then displayed.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles