Structure & Union
Structure & Union
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
2nd way:
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.
C Structure example
Let's see a simple example of structure in C language
C Array of Structure
Why use an array of structures?
Consider a case, where we need to store the data of 5 students. We can store it by using the
structure as given below.
OUTPUT
In the above program, we have stored data of 5 students in the structure. However, the
complexity of the program will be increased if there are 20 students. In that case, we will
have to declare 20 different structure variables and store them one by one. This will always
be tough since we will have to declare a variable every time we add a student. Remembering
the name of all the variables is also a very tricky task. However, c enables us to declare an
array of structures by using which, we can avoid declaring the different structure variables;
instead we can make a collection containing all the structures that store the information of
different entities.
Array of Structures in C
An array of structres in C
can be defined as the collection of multiple structures variables where each variable contains
information about different entities. The array of structures in C
are used to store information about multiple entities of different data types. The array of
structures is also known as the collection of structures.
Nested Structure in C
C provides us the feature of nesting one structure within another structure by using which,
complex data types are created. For example, we may need to store the address of an entity
employee in a structure. The attribute address may also have the subparts as street number,
city, state, and pin code. Hence, to store the address of the employee, we need to store the
address of the employee into a separate structure and nest the structure address into the
structure employee. Consider the following program.
1. By separate structure
2. By Embedded structure
1) Separate structure
Here, we create two structures, but the dependent structure should be used inside the main
structure as a member. Consider the following example.
As you can see, doj (date of joining) is the variable of type Date. Here doj is used as a member
in Employee structure. In this way, we can use Date structure in many structures.
2) Embedded structure
The embedded structure enables us to declare the structure inside the structure. Hence, it
requires less line of codes but it can not be used in multiple data structures. Consider the
following example.
C Nested Structure example
Let's see a simple example of the nested structure in C language.
OUTPUT
Pointer to structure
Union in C
is a special data type available in C that allows storing different data types in the same
memory location. You can define a union with many members, but only one member can
contain a value at any given time. Unions provide an efficient way of using the same
memory location for multiple purposes.