In C programming, the structure is user-defined data types that allow us to store the combination of different data types together. All the different data type variables are represented by a single name.
As we have studied earlier that Array can hold only a similar type of variable in contiguous memory locations. Similarly, a structure can hold several data of various kinds in a contiguous memory location.
Why to use Structure?
Suppose, let say I want to store different attributes of different data types. For example, i want to store the information about the student which contain name(string), roll_no(int), address(string), Score(float), etc. All these are of different types. So to store these various kinds of attributes you need to create all these again and again for each student. And that would be very time-consuming and a headache to create the same variables again and again.
That is when structure comes into action. With the help of structure now I can create the members for the structure (members refers to the name, roll_no, address, etc.). And create the variables for each student. Go through the example below, you will understand.
Structures are variables that have several parts, each part of the object can have different types. Each part of the structure is called a member of the structure.
Syntax:
The keyword used to define the structure is struct
.
1 2 3 4 5 6 7 8 | struct structure_name { data_type member1; data_type member2; data_type member3; ... ... }; |
Consider basic data of student: name, roll_no, address, score.
student structure is defined in the following way:
1 2 3 4 5 6 7 8 | struct student { char name[30]; int roll_no; char address[5O]; float score; }; |
Declaring Structure variable
A structure variable is required to access the members of the structure. There are two ways to declare structure variable in C,
1st way:
Let say there are the following members of structure student.
1 2 3 4 5 6 7 | struct student { char name[30]; int roll_no; char address[5O]; float score; }; |
And now to create a structure variable for the above student structure, the following line needs to be written inside the main function.
struct student s1, s2;
s1 and s2 are used to access the member of the student. These are like the objects for the student just like an object in C++.
2nd Way:
Another method is to declare the objects right after defining the structure as shown below.
1 2 3 4 5 6 7 | struct student { char name[30]; int roll_no; char address[5O]; float score; } s1,s2; //struct varibale created |
Which one to use 1st way or 2nd way?
- Use the 1st way, if the number of variables is not fixed because creating the variable in the main() function gives you more flexibility.
- Use the 2nd way, if the number of variables that you need to use is fixed.
Scope:
A structure type declaration can be local or global, depending upon where the declaration is made.
Access members of a structure
There are two ways to access the members of the structure.
.
– (Member or Dot operator)->
– (Structure pointer operator), discussed in the next.
1. .
– (Member or Dot operator)
Let say that I want to access the member name of s1 by (.
) Dot operator. This can be done in the following way:
s1.name;
C Structure program
1. Example to store information for more than one student in a structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include<stdio.h> #include <string.h> struct student { char name[30]; int roll_no; char address[40]; float score; }s1, s2; //declaring structure variable int main() { //storing student 1 Info strcpy(s1.name, "Sherlock Holmes"); s1.roll_no = 101; strcpy(s1.address, "221B Baker Street"); s1.score = 91.10; //storing student 2 Info strcpy(s2.name, "James Moriarity"); s2.roll_no = 102; strcpy(s2.address, "13 Street, Ireland"); s2.score = 89.05; //printing student 1 Info printf( "Student 1 Name: %s\n", s1.name); printf( "Student 1 Roll No: %d\n", s1.roll_no); printf( "Student 1 Address: %s\n", s1.address); printf( "Student 1 Score: %.2f\n", s1.score); //printing student 2 Info printf( "\nStudent 2 Name: %s\n", s2.name); printf( "Student 2 Roll No: %d\n", s2.roll_no); printf( "Student 2 Address: %s\n", s2.address); printf( "Student 2 Score: %.2f\n", s2.score); return 0; } |
Output:
Student 1 Name: Sherlock Holmes
Student 1 Roll No: 101
Student 1 Address: 221B Baker Street
Student 1 Score: 91.10
Student 2 Name: James Moriarity
Student 2 Roll No: 102
Student 2 Address: 13 Street, Ireland
Student 2 Score: 89.05
2. Example to store information by taking user input and adding two numbers in structure in c.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include<stdio.h> struct student { char name[30]; float maths; float science; float chemistry; float total; }s1, s2; //declaring structure variable int main() { //Taking student 1 and 2 Name printf("Enter Student 1 Name:\n"); gets(s1.name); printf("Enter Student 2 Name:\n"); gets(s2.name); //taking s1 and s2 marks printf("\nEnter Student 1 marks on Maths, Science and Chemistry respectively:\n"); scanf("%f %f %f", &s1.maths, &s1.science, &s1.chemistry); printf("Enter Student 2 marks on Maths, Science and Chemistry score respectively:\n"); scanf("%f %f %f", &s2.maths, &s2.science, &s2.chemistry); //Adding s1 and s2 marks s1.total = s1.maths + s1.science + s1.chemistry; s2.total = s2.maths + s2.science + s2.chemistry; //comparing the total if(s1.total > s2.total) printf("\n%s scored the highest: %.2f", s1.name, s1.total); else if(s1.total < s2.total) printf("%s scored the highest: %.2f", s2.name, s2.total); else printf("%s and %s scored equal: %.2f", s1.name, s2.name, s1.total); return 0; } |
Output:
Enter Student 1 Name:
Sherlock Holmes
Enter Student 2 Name:
Jim Moriarty
Enter Student 1 marks on Maths, Science and Chemistry respectively:
80.05 70 88.15
Enter Student 2 marks on Maths, Science and Chemistry respectively:
79.15 72.35 82
Sherlock Holmes scored the highest: 238.20
C – Use of typedef keyword in Structure
typedef in Structure is used to simplify the code to improve readability and confusion. In other words, it simplifies the syntax of structure
structure without the use of typedef,
1 2 3 4 5 6 7 8 9 10 11 12 | struct student { char name[30]; int roll_no; char address[5O]; float score; }; int main() { struct student s1, s2; } |
Now let us see the structure syntax with the use of typedef,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | typedef struct student { char name[30]; int roll_no; char address[5O]; float score; }stud; int main() { stud s1; ... ... } |
As you can see, now stud
is declared as the variables of struct
student and with the help of stud
variable, we can create the variables of type struct student.
Example of structure using typedef keyword:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> typedef struct student { char stud_name[20]; int roll_no; }stud; int main() { stud s1; //user inputs printf("Enter the name of the student: "); scanf("%[^\n]s",&s1.stud_name); printf("Enter the roll no student: "); scanf("%d",&s1.roll_no); //display the detail printf("\nName of the student: %s\n", s1.stud_name); printf("Roll Number of the Student: %d", s1.roll_no); return 0; } |
Output:
Enter the name of the student: James Sebastian
Enter the roll no student: 18
Name of the student: James Sebastian
Roll Number of the Student: 18