codescracker


c++

C++ Structure Array



« Previous Tutorial Next Tutorial »


The structure and the array both are C++ derived types. While arrays are collections of analogous elements, structures assemble dissimilar elements under one roof. Thus both the array and the structure allow several values to be treated together as a single data object.

The arrays and structures can be combined together 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 having 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 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 chapters), we will write :

addr mem_addr [100];

This creates 100 sets of variables that are organised 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 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 emp type which is a nested structure (already defined in previous chapter) :

emp sales_emp[100] ;

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

C++ Structure Array Example

Here is an example program, demonstrating structure array (array of structure ) in C++. This program creates an array of structures evar, reads information into it and displays the information of an employee depending upon the given empno :

/* C++ Structure Array - C++ Array of Structure */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

struct addr
{
	int houseno;
	char street[30];
	char city[20];
	char state[20];
};
struct emp
{
	int empno;
	char name[20];
	char desig[20];
	addr address;
	float basic;
}worker;

void main()
{
	clrscr();

	emp evar[5];
	int i;
	char ch;

	for(i=0; i<5; i++)
	{
		cout<<"Employee No: ";
		cin>>evar[i].empno;
		cout<<"Employee Name: ";
		gets(evar[i].name);
		cout<<"Designation: ";
		gets(evar[i].desig);
		cout<<"House No: ";
		cin>>evar[i].address.houseno;
		cout<<"Street: ";
		gets(evar[i].address.street);
		cout<<"City: ";
		gets(evar[i].address.city);
		cout<<"State: ";
		gets(evar[i].address.state);
		cout<<"Basic Pay: ";
		cin>>evar[i].basic;
		cout<<"\n";
	}

	clrscr();

	do
	{
		cout<<"Enter employee no. whose information is to be displayed:\n";
		int eno;
		cin>>eno;
		int flag=0;
		for(i=0; i<5; i++)
		{
			if(evar[i].empno == eno)
			{
				flag = 1;
				cout<<"\nEmployee Data:\n";
				cout<<"Employee No: "<<evar[i].empno;
				cout<<"\nName: "<<evar[i].name;
				cout<<"\nDesignation: "<<evar[i].desig;
				cout<<"\nAddress: "<<evar[i].address.houseno<<", ";
				cout<<evar[i].address.street<<", ";
				cout<<evar[i].address.city<<", ";
				cout<<evar[i].address.state<<endl;
				cout<<"Basic Pay: "<<evar[i].basic;
				cout<<"\n";
				break;
			}
		}
		if(!flag)
		{
			cout<<"\nSorry..!!..No such employee exists..!!\n";
			cout<<"Exiting.....Press a key...\n";
			getch();
			exit(1);
		}
		cout<<"Want to display more ? (y/n).. ";
		cin>>ch;
	}while(ch=='y' || ch=='Y');
	getch();
}

Here are some sample runs of the above C++ program. Feeding data into the structure, shown in the following three figures:

c++ structure array

c++ array of structure

structure array c++

After feeding the data into the structure, as shown in the above three figures. Now press ENTER, and then enter the employe number whose information, you want to check, as shown in the following figures:

array of structure c++

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 be kept in mind is that, to access it, its structure name followed by a dot (.) and the array name is to be given. For example, consider this structure :

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

The above declared structure variable learner is of structure type student that contains an element which is an array of 5 floats to store marks of a student in 5 different subjects. To reference marks of 3rd subject of structure learner, we will write :

learner.marks[2] ;

See, to access elements of array marks which is member of learner structure, array's complete name i.e., learner.marks is being used.

the array in a structure may even be two-dimensional as it is shown below :

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

To reference integer 2, 4 in x of structure svar, we shall write :

svar.x[2][4] ;

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test