Table of Contents
Introduction
We have studied arrays that are meant to store data of the same type. But what about the data that contains different types of information like name (char), age(int), weight(float). Now all these parameters are of different data types, and we cannot store them in a single array. So, have you ever thought if there is any way to store dissimilar data?
Structures in the C language are used to store data of different data types.
For example, there are 10 students in a class, and the teacher wants to calculate the BMI of every student. To calculate the BMI, we need name, age, height, weight of every student as data. Since ‘name’ is of type char (string), ‘age’ is an int type, and ‘height’ and ‘weight’ are of float type. It is impossible to store this data in an array since we can make an array for students, but these parameters can’t be stored in a single array. Here, we can make the structure to store data of different types.
Let’s see how to use a structure in our code with its proper definition and declaration.
Defining a structure
Syntax:
struct structure_name { data-type member-1; data-type member-2; data-type member-3; data-type member-4; };
‘struct’ is the keyword used before the structure name. ‘members’ are nothing else but the variables of a particular ‘data type’.
To calculate the BMI of students, let us name the structure as ‘student’. In the structure’s body, the parameters will be like ‘char name[];’ where ‘char’ is the type of ‘name’ member. Similarly, we can write ‘int age;’, ‘float weight;’ etc. So, our structure will look like this:
struct student { char name[15]; int age; float weight; };
Declaration of variables for structure
While using the structure, some variables need to be declared in the ‘main’ function just as we declare variables of type int, char, etc.
Let us understand this by continuing the same example discussed above. We have defined a structure named ‘student’ with certain members like ‘name’, ‘age’, ‘weight’. Basically, all these parameters are of a student. Suppose we want to calculate the BMI of 3 students so that we will declare three variables, say ‘s1’, ‘s2’, ‘s3’ in the main function, representing three students of the structure student’.
The declaration can be made as:
struct student s1, s2, s3;
Here, ‘struct student’ is acting as a data type for the variables s1, s2, s3.
Thus, the whole code up to here will look like this:
struct student { char name[15]; int age; float weight; }; int main() { struct student s1, s2, s3; }
We may also declare an array of this structure if the number of students increases.
The variables can be declared in another way by declaring them at the time of defining structure.
struct student { char name[15]; int age; float weight; } s1, s2, s3;
So now we are ready with the tools that will help us calculate students’ BMI easily. We have to enter the details of each student to get the desired output.
Suppose we want to enter the age of the first student. For this, we need to access the ‘age’ member of the structure by the s1 variable. We do this by writing,
s1.age=20;
Dot (.) operator is used to access the ‘age’ member for ‘s1’ student. ‘s1.age’ means the age of s1.
Similarly, if we want to enter the name of the student ‘s1’ then the statement will be written as,
strcpy(s1.name, “Thomson”);
‘s1.name’ means the name of s1. Since we can’t directly assign a string value to a variable thus, we use the ‘strcpy’ function that will copy ‘Thomson’ in the ‘s1.name’.
We have discussed all the basic concepts which are needed for the use of structures. It’s time to write the complete code.
Program
#include<stdio.h> #include<string.h> struct student { char name[20]; int age; float weight; float height; }; int main() { printf("Details of the student are as below:\n"); struct student s1={"Brown",20,65.5,172.72 }; printf("\nFirst Student\n"); printf("Name:%s\n",s1.name); printf("Age:%d\n",s1.age); printf("Height:%.2f\n",s1.height); printf("Weight:%.2f\n",s1.weight); struct student s2; strcpy(s2.name,"Thomson"); s2.age=21; s2.weight=70; s2.height=175.0; printf("\nSecond Student\n"); printf("Name:%s\n",s2.name); printf("Age:%d\n",s2.age); printf("Height:%.2f\n",s2.height); printf("Weight:%.2f\n",s2.weight); return 0; }
Output
Explanation
The definition of structure ‘student’ is made in the above program. In the ‘main’ function we have declared two variables ‘s1’ and ‘s2’ but the values are assigned differently to both the variables.
struct student s1= {“Brown”,20, 65.5, 172.72};
This is the one way to declare a variable for the structure and initializing it simultaneously. ‘s1’ is declared and assigned with the values in the same order as the members are declared in the body of the structure.
Rest all ‘printf()’ statements are printing the values of these variables line by line.
The other way is to declare the variable first and assign a value to each variable.
‘s2’ declared first and then the statement
strcpy(s2.name,”Thomson”);,
s2.age=21;
s2.weight=70;
s2.height=175.0;
are used to assign values.
Thus, both the ways are valid enough and we can use any of them.
Note: Always assign a string value to a string variable by using ‘strcpy’. Since we cannot equate two strings in C. ‘s1.name= “Brown”;’ is completely invalid and will show an error. Thus, using the ‘strcpy’ function we can assign value by writing ‘strcpy(s1.name, “Brown”)’.
0 Comments