Unit 7
Unit 7
Structure
At times, during programming, there is a need to store multiple logically related elements
under one roof. For instance, an employee’s details like name, employee number, and
designation need to be stored together. In such cases, the C language provides structures to do
the job for us.
A structure can be defined as a single entity holding variables of different data types that are
logically related to each other. All the data members inside a structure are accessible to the
functions defined outside the structure. To access the data members in the main function, you
need to create a structure variable.
Structure in C programming is very helpful in cases where we need to store similar data of
multiple entities. Let us understand the need for structures with a real-life example. Suppose
you need to manage the record of books in a library. Now a book can have properties like
book_name, author_name, and genre. So, for any book you need three variables to store its
records. Now, there are two ways to achieve this goal.
The first and the naive one is to create separate variables for each book. But creating so many
variables and assigning values to each of them is impractical. So what would be the
optimized and ideal approach? Here, comes the structure in the picture.
We can define a structure where we can declare the data members of different data types
according to our needs. In this case, a structure named BOOK can be created having three
members book_name, author_name, and genre. Multiple variables of the type BOOK can be
created such as book1, book2, and so on (each will have its own copy of the three members
book_name, author_name, and genre).
Syntax to Define a Structure in C
struct structName
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
};
Keyword struct: The keyword struct is used at the beginning while defining a structure in
C. Similar to a union, a structure also starts with a keyword.
structName: This is the name of the structure which is specified after the keyword struct.
data_Type: The data type indicates the type of the data members of the structure. A
structure can have data members of different data types.
member_name: This is the name of the data member of the structure. Any number of data
members can be defined inside a structure. Each data member is allocated a separate space
in the memory.
Variables of the structure type can be created in C. These variables are allocated a separate
copy of data members of the structure. There are two ways to create a structure variable in C.
This way of declaring a structure variable is suitable when there are few variables to be
declared.
Syntax
struct structName
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
} struct_var1, struct_var2;
Example
struct bookStore
// structure definition
char storeName
int totalBooks;
char storeLicense[20];
The structure variables are declared at the end of the structure definition, right before
terminating the structure. In the above example, storeA and storeB are the variables of the
structure bookStore. These variables will be allocated separate copies of the structure’s data
members that are- storeName, totalBooks, and storeLicense.
How to Initialize Structure Members?
Structure members cannot be initialized like other variables inside the structure definition.
This is because when a structure is defined, no memory is allocated to the structure’s data
members at this point. Memory is only allocated when a structure variable is declared.
Consider the following code snippet.
struct rectangle
// structure definition
};
A compilation error will be thrown when the data members of the structure are initialized
inside the structure.
To initialize a structure’s data member, create a structure variable. This variable can access
all the members of the structure and modify their values. Consider the following example
which initializes a structure’s members using the structure variables.
struct rectangle
// structure definition
int length;
int breadth;
};
int main()
{
struct rectangle my_rect; // structure variables;
my_rect.length = 10;
my_rect.breadth = 6;
In the above example, the structure variable my_rect is modifying the data members of the
structure. The data members are separately available to my_rect as a copy. Any other
structure variable will get its own copy, and it will be able to modify its version of the length
and breadth.
The members of a structure are accessed outside the structure by the structure variables using
the dot operator (.). The following syntax is used to access any member of a structure by its
variable:
Syntax
structVariable.structMember
Example
The following example illustrates how to access the members of a structure and modify them
in C.
#include <stdio.h>
#include <string.h>
struct cube
// data members
char P_name[10];
int P_age;
char P_gender;
};
int main()
// structure variables
strcpy(p1.P_name, "XYZ");
p1.P_age = 25;
p1.P_gender = 'M';
strcpy(p2.P_name, "ABC");
p2.P_age = 50;
p2.P_gender = 'F';
// patient 1
if (p1.P_gender == 'M')
}
else
printf("\n");
// patient 2
if (p2.P_gender == 'M')
else
return 0;
struct student
{
char name[50];
char class[100];
int roll_number;
float marks[5];
};
Before, we created 3 different variables to store 3 students. Now all this can be done
efficiently in one single array.
Hence, C enables us to declare an array of structures. We can evade declaring the different
structure variables
As you can see in the below image, S is array of structure student having size 10 which
means it could store information of 10 different variables of type student. So we don't need to
take 10 different variables instead we could use array of structure student.
Conclusion
A Structure is a data type that gives us the ability to allow a collection of
interconnected variables to be treated as a single unit instead of distinct entities.
There are two ways of declaring the structure variable i.e with and without the
structure declaration.
An array of structure is much more efficient than declaring multiple variables
separately.
An array of structures written in C can be described as a collection of numerous
structure variables containing data about different entities.