“In C Language, Structures are combined data type initialization that is used to group multiple variables in a single type; variables that are grouped must be related to each other. It allows those variables to be accessed by a single pointer. The main difference between a Structure and an Array is that an Array has only a single datatype, but on the other hand, a Structure can hold different datatypes. Hence, we can say that structure is a user-defined datatype that is used to store multiple variables with different datatypes in a single block to store a specific type or record.
Let’s suppose we need to store a record of a person; that type of record will have attributes with different datatypes like Number, Name in Char, etc. For that purpose, we cannot use Array as it stores records with the same data type. The structure will come in handy in this case for us. It is a way to different group datatypes, and defining it means we are creating our new datatype.”
The syntax to declare a Structure is as follows:
In the above figure, we have written a piece of code to declare a structure. Struct is a keyword to let the system know that we are initializing a Structure. After that, we assigned a name to our Structure that is “Structer_Name”. Then an opening bracket to declare our variables. In the variable section, we have declared variables of different datatypes to guide you about how a structure can hold together variables with different datatypes. The variables are therefore included in the structure. To end our structure block, we use a semicolon as a statement breaker.
In this illustration, a Structure called “Student Details” has been constructed. In the variables block, we have declared a few variables describing the attributes a student has. Then we have taken three traits of a student, his name, roll number, and result or, in our example, percentage. The roll numbers datatype is an integer because usually, a roll number is a number. The full name is assigned a character, and at last, the percentage is allocated the Float datatype because we can get the percentage in decimal points.
One thing to keep in mind is that the structure cannot be declared in the main method. It can be called using its object in the main method. After declaring the Structure, we will go into our main method in which we will declare an object of our structure. With the help of that object, we can perform operations on the variables.
Detail1 is the name of an object we’ve generated. For each student, we will define a different object of our Structure like Detail2, Detail3, etc. With the help of Detail1, we called variables of our structure. Now, Detail1 will create a record of a specific student. We have assigned Roll_num=100 to our student and all the other attributes as well. To assign the data to a specific record, we will use the object created for that specific record.
After assigning all the values to the variable in Instance Detail1 of our structure, we will display those records with the help of the printf function. In the printf function, %d denotes decimal values, %s represents string values, and %f tells that it will display the float values. We have printed all three attributes of “Student XYZ”.
After running the program, we will get this output of our above-written code. We assigned the values to a specific instance of our Structure and printed the values, and as you can see in the picture below that, the system has displayed us the values of that instance. Additional procedures can be carried out on these values. You can also define a different instance of the Student_Details Structure to create records of multiple students with different names and other traits.
In the following example, a structure called a Table has been constructed. The table has a length and a width, so we have taken them as parameters of this structure. We have already explained in the earlier example how a structure stores different data types. But in this example, we will explain how we can access multiple instances of a single Structure. In the body of the structure, we have declared two integer variables.
Now to call the structure, we will go to our main function. In the main function, we have defined two integers as area1 and area2, which will store the area of table1 and table2, respectively. We have created the first object of our table as t1, which will act as table1, and we have declared the second instance of our structure as t2, which will act as table2. Table 1’s length is given a value of five, and its breadth is three. Area1 variable will store the value we will get by performing a multiply operation on our t1 integers. The values assigned to table2 are 7 and 5. Area2 will store the output from multiplying these values with each other. At last, we printed the output to get our results from the code.
The system will give this as an output. We assigned 5 and 3 to our t1 instance, and with the help of the object of our structure, we performed a mathematical operation on our data.
We assigned 7 and 5 to our t2 object, and it also gave us output after the operation. So, it is clear that with the help of a single structure and two variables, we operated on multiple objects. First, we calculated the area of Table 1 and then the area of Table2. This is an instance to show how the instances and objects of structure work.
In this guide, we have discussed how the Structures are implemented in C Language. You can learn more about the structures by using multiple scenario-based examples. You can even get hold of it by practicing examples with different cases. Structures enable us to use their instances as many times as we need and to create as many records as we want. On top of that, they not only allow you to create multiple instances and different datatypes but also allows you to perform all the mathematics, arithmetics, and other operations related to those datatypes. It is an efficient approach to handling multiple records.