Introduction

A variable of a certain data type such as int or char can store only one element of that data type. An array, on the other hand, can store multiple elements of a fixed data type. An array, thus, allows a programmer to address a collection of similar elements by a single name, while also allowing him to address each of the elements by their index in the array.

Can we create a collection of data elements of various data types and refer to this collection by a single name? The answer to this question is 'yes' and this is possible by the use of structures. A structure allows you to make a collection of various elements of different data types and refer to them by a single name. A structure can contain arrays as well. Hence, a structure in some sense acts like a new data type which has multiple components of various other data types. So, it is possible to make an array of structure variables also.

Theory

Arrays are used to store large sets of data and manipulate them but the disadvantage is that all the elements stored in an array are to be of the same data type. When we need a collection of different data items of different data types, such as integer, float etc., we can use a structure. Structure can be seen as datatype composed of different datatypes. For example, suppose you want to store information about students enrolling in a university. Then, you may naturally want to store information like Student Name, Roll Number, Gender, Batch, etc. Ofcourse, you can create separate arrays for storing each of these quantities, but notice that for each student this diverse data is highly related. So, using a structre to pack these data entries into a single variables would be a good choice. In this case we can define the structure as,

```            struct student_record{
char Name;
int Roll;
char gender;
char Stream;
};
```

This definition will practically create a new datatype students_record having a 2 character arrays, one integer and one character. So, in the main function one can define variables of this new compound datatype as,

```            student_record student1, student2;
```

You can even define an array of structure variables like,

```            students_record students;
```

Now, the individual elements of a structure variable can be addressed by the dot(.) operator. For example, the variable student1 can initialized using the following statements:

```            strcpy(student1.Name,"Abc");
student1.Roll=24;
student1.gender='m';
strcpy(student1.Stream,"Computer Science");
```

An interesting thing to note is that the memory allocation for the whole structure is done contigously. So, size of one variable of type student_record is 100+4+1+100=205 bytes. And, if r1 and r2 are two variables of the structure struct_record, then writing r1=r2 is equivalent to copying the data in 205 bytes corresponding to r2 and copying them into r1.

Objective

1. To understand how to use structures as a compund datatype.
2. To be able to design structures according to your requirement.
3. To understand when structures are better datastructures than arrays.

Manual

In this experiment you will understand how to define structures according to problem requirements. You will also do operations on structure elements.

Procedure

1. Press start to start the experiment and select a value of N.
2. Press next to see the execution of the code
3. Relavant line in the code is shown here
4. The output of the code is shown in the right