Array of structure





Array of structure

It is well known that array is a collection of similar type of data’s referenced by a common name.

The combination having structures within an array is an array of structures. Or, a similar type o structure placed in a common heading or a common variable name is called array of structure.

To declare 10 element array of structure of type school (defined earlier), we will write:-

School student [10];

E.g.

A program to read name, roll no, address and weight of ‘n’ students and displaying them using structure.

 

#include<iostream.h>

#include<conio.h>

#defie max 100

Struct School

{

Int roll;

Char name [25];

Char address [25];

Float weight;

} student [max];

Void main ()

{

Int I, n;

Clrscr ();

Cout<<”\n How many students?”;

Cin>>n;

For (i=0; i<n; i++)

{

Cout<<”\n Enter name”;

Cin>>stu[i].name;

Cout<<”\n Enter roll”;

Cin>>stu[i].roll;

Cout<”\n enter address”;

Cin>>stu[i].address;

Cout<<”\n enter weight”;

Cin>>stu[i].weight;

Cout<<”\n the details of students are:-“;

Cout<<”\n name \rollno\address\weight”;

For (i=0; i<n; i++)

{

Cout<<”\n”<<stu[i].name;

Cout<<”\n”<<stu[i].roll;

Cout<<”\n”<<stu[i].address;

Cout<<”\n”<<stu[i].weight;

}

getch ();

}

 

 Structures within a structure (Related structure)

A structure can be nested inside another structure when a structure is declared as the member of another structure, it is called structure within a structure or nested structure.

For example, the nested structure can be declared in the following format:-

Struct date

{

Int day;

Int month;

Int year;

}

Struct student

{

Char name [25];

Int roll;

Char address [25];

}



Related posts:

  1. Structure in C++ Structure in C++ Structure is a collection of simple variables...
  2. Array Of Class Object Array of class objects Array is a collection of similar...
  3. String In C++ String A string is an array of type char. Declaration:- ...
  4. Pointer , New and Delete in C++ Pointer A pointer is a variable that contains address of...
  5. Passing Arrays To Functions Passing arrays to functions Array can be passed as argument...