Structures in C
In this class, we will try to understand Structures in C.
Before understanding the concept of structure, we will recap the idea of the aggregate variables, as shown in the image below.
Aggregate variables are the variables that can store more than one value.
We have discussed that array and structure are aggregate variables.
Table of Contents
Structures in C
The only difference between the arrays and structures is the elements of the array are of the same type, but the members of the structure may be of different types.
When we need to store a collection of related items, a structure is a logical choice.
For example, if we want to collect students’ data, then the data items are name, number, phone numbers are the data related to the students.
In such cases, structures are the right choice.
Declaration of a Structure
The image below is how a structure can be declared.
The struct is the keyword used to declare a structure.
Within the enclosed curly braces are the members of the structures.
The stu1 and stu2 are the variables of the structures.
For each variable, the compiler will create the memory for all the members.
The image below is how the compiler will create the memory for stu1 and stu2.