Structure

We have already dealt with arrays. Arrays are used to store similar type of data. Have you ever thought if there is any way to store dissimilar data?

The answer is yes. We use structures to store different types of data. For example, you are a student. Your name is a string and your phone number and roll_no are integers. So, here name, address and phone number are those different types of data. Here, structure comes in the picture.

Defining a Structure

In our case, let's name the structure as student. The members of the structure in our case are name, roll_no and phone_number.

So, our structure will look like:

struct student
{
int roll_no;
std::string name;
int phone_number;
};

Declaration of Structure Variable

Just as we declare variables of type int, char etc, we can declare variables of a structure as well.

Suppose we want to store the roll no, name and phone number of three students. For this, we will define a structure named student (as declared above) and then declare three variables, say p1, p2 and p3 (which will represent the three students respectively) of type 'student'. This declaration will be done in the main function.

First Student
roll no : 1
name : Brown
phone no : 123443
Second Student
roll no : 2
name : Sam
phone no : 1234567822
Third Student
roll no : 3
name : Addy
phone no : 1234567844

struct student p1 = {1,"Brown",123443}; - This line is just to show that we can also initialize a structure in this way.

In the next line, we are just giving values to the variables and printing those.

Structures use continuous memory locations.

Array of Structures

We can also make an array of structures. In the first example in structures, we stored the data of 3 students. Now suppose we need to store the data of 100 such children. Declaring 100 separate variables of the structure is definitely not a good option. For that, we need to create an array of structures.

Here we created an array named stud having 5 elements of structure student. Each of the element stores the information of a student. For example, stud[0] stores the information of the first student, stud[1] for the second and so on.

In this example, we are printing roll number, name and phone number of a student using a function. We first declared a structure named student with roll_no, name and phone number as its members and 's' as its variable. Then we assigned the values of roll number, name and phone number to the structure variable s. Just as we pass any other variable to a function, we passed the structure variable 's' to a function 'display'.

Now, while defining the function, we passed a copy of the variable 's' as its argument with 'struct student' written before it because the variable which we have passed is of type structure named student. Finally, in the function, we printed the name, roll number and phone number of the structure variable.

Passing by Reference

In passing by reference, the address of a structure variable is passed to a function. In this, if we change the structure variable which is inside the function, the original structure variable which is used for calling the function changes. This was not the case in calling by value.

This case is similar to the previous one, the only difference is that this time, we are passing the address of the structure variable to the function. While declaring the function, we passed the pointer of the copy 'st' of the structure variable 's' in its parameter. Since the pointer is of a variable of type structure named student, we wrote 'struct student' before the name of the pointer in the argument of the function. In the function , we accessed the members of the pointer using -> sign as discussed before.

Try to change the value of the variables inside the function in both the cases and see the changes in the real variables.