Why use structure?
In C, there are cases where we need to store multiple attributes of an entity. It is not necessary that an entity has all the information of one type only. It can have different attributes of different data types. For example, an entity Student may have its name (string), roll number (int), marks (float). To store such type of information regarding an entity student, we have the following approaches:
Let's look at the first approach in detail.
Enter the name, roll number, and marks of the student 1Arun 90 91 Enter the name, roll number, and marks of the student 2Varun 91 56 Enter the name, roll number, and marks of the student 3Sham 89 69 Printing the Student details... Arun 90 91.000000 Varun 91 56.000000 Sham 89 69.000000
The above program may fulfill our requirement of storing the information of an entity student. However, the program is very complex, and the complexity increase with the amount of the input. The elements of each of the array are stored contiguously, but all the arrays may not be stored contiguously in the memory. C provides you with an additional and simpler approach where you can use a special data structure, i.e., structure, in which, you can group all the information of different data type regarding an entity.
What is Structure
Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member. Structures ca; simulate the use of classes and templates as it can store various information
The ,struct keyword is used to define the structure. Let's see the syntax to define the structure in c.
Let's see the example to define a structure for an entity employee in c.
The following image shows the memory allocation of the structure employee that is defined in the above example.
Here, struct is the keyword; employee is the name of the structure; id, name, and salary are the members or fields of the structure. Let's understand it by the diagram given below:
Declaring structure variable
We can declare a variable for the structure so that we can access the member of the structure easily. There are two ways to declare structure variable:
Let's see the example to declare the structure variable by struct keyword. It should be declared within the main function.
Now write given code inside the main() function.
Let's see another way to declare variable at the time of defining the structure.
Which approach is good
If number of variables are not fixed, use the 1st approach. It provides you the flexibility to declare the structure variable many times.
If no. of variables are fixed, use 2nd approach. It saves your code to declare a variable in main() function.
Accessing members of the structure
There are two ways to access structure members:
Let's see the code to access the id member of p1 variable by. (member) operator.
C Structure example
Let's see a simple example of structure in C language.
employee 1 id : 101 employee 1 name : Sonoo Jaiswal
Let's see another example of the structure in C language to store many employees information.
employee 1 id : 101 employee 1 name : Sonoo Jaiswal employee 1 salary : 56000.000000 employee 2 id : 102 employee 2 name : James Bond employee 2 salary : 126000.000000