Structures
8.1 Introduction
A structure is similar to records. It stores related information about an entity.
Structure is basically a user-defined data type that can store related information (even of
different data types) together.
The major difference between a structure and an array is that, an array contains related
information of the same data type.
A structure is, therefore, a collection of variables under a single name. The variables within a
structure are of different data types and each has a name that is used to select it from the
structure.
8.1.1 Structure Declaration
A structure is declared using the keyword struct followed by a structure name.
All the variables of the structure are declared within the structure.
A structure type is generally declared by using the following syntax:
struct struct-name
{
data_type var-name;
};
For example, if we have to define a structure for a student, then its related information probably would
be: roll_number, name, course, and fees. This structure can be declared as:
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
Now, the structure has become a user-defined data type.
Each variable name declared within a structure is called a member of the structure.
The structure declaration, however, does not allocate any memory or consume storage space.
It just gives a template that conveys to the C compiler how the structure is laid out in memory
and gives details of the member names.
Like any other data type, memory is allocated for the structure when we declare a variable of
the structure.
For example, we can define a variable student by writing:
struct student stud1;
Here, struct student is a data type and stud1 is a variable. Alternatively, variables can be declared at the
time of structure declaration:
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
} stud1, stud2;
When we declare variables of the structure, separate memory is allocated for each variable.
Structure type and variable declaration of a structure can be either local or global depending on
their placement in the code.
Care should be taken to ensure that the name of a structure and the name of a structure
member should not be the same.
Moreover, structure name and its variable name should also be different.
Programming Tip: Do not forget to place a semicolon after the definition of structures and
unions.
8.1.2 Typedef Declarations
The typedef (derived from type definition) keyword enables the programmer to create a new
data type name for an existing data type.
By using typedef, no new data is created, rather an alternate name is given to a known data
type. The general syntax of using the typedef keyword is given as:
typedef existing_data_type new_data_type;
The typedef statement does not occupy any memory, it simply defines a new type. For example:
typedef int INTEGER;
INTEGER num = 5;
When we precede a struct name with typedef keyword, then the struct becomes a new type.
It is used to make the construct shorter with more meaningful names for types already defined
by C or for types that you have declared.
A typedef declaration is a synonym for the type. For example, writing:
typedef struct student
{
int r_no;
char name[20];
char course[20];
float fees;
} student;
Now that you have preceded the structure's name with the keyword typedef, the student
becomes a new data type.
Therefore, now you can straightaway declare variables of this new data type as you declare
variables of type int, float, char, double, etc. To declare a variable of structure student you will
just write:
student stud1;
Programming Tip: C does not allow declaration of variables at the time of creating a
typedef definition. So variables must be declared in an independent statement.
8.1.3 Initialization of Structures
A structure can be initialized in the same way as other data types are initialized.
Initializing a structure means assigning some constants to the members of the structure.
When the user does not initialize the structure, then C automatically does that.
For int and float members, the values are initialized to zero and char and string members are
initialized to '\0' by default (in the absence of any initialization done by the user).
The initializers are enclosed in braces and are separated by commas.
However, care must be taken to see that the initializers match their corresponding types in the
structure definition.
The general syntax to initialize a structure variable is given as follows:
struct struct_name
{
data_type member_name1;
data_type member_name2;
data_type member_name3;
};
struct struct_name struct_var = {constant1, constant2,
constant3, ...};
For example, we can initialize a student structure by writing:
struct student stud1 = {01, "Rahul", "BCA", 45000};
When all the members of a structure are not initialized, it is called partial initialization.
In case of partial initialization, first few members of the structure are initialized and those that
are uninitialized are assigned default values. For example:
struct student stud2 = {07, "Rahul"};
This leaves course initialized to '\0' and fees initialized to 0.0.
8.1.4 Accessing the Members of a Structure
Each member of a structure can be used just like a normal variable, but its name will be a bit
longer.
A structure member variable is generally accessed using a . (dot) operator.
The syntax of accessing a structure or a member of a structure can be given as:
struct_var.member_name
The dot operator is used to select a particular member of the structure.
For example, to assign values to the individual data members of the structure variable stud1, we
may write:
stud1.r_no = 01;
[Link] = "Rahul";
[Link] = "BCA";
[Link] = 45000;
To input values for data members of the structure variable stud1, we may write:
scanf("%d", &stud1.r_no);
scanf("%s", [Link]);
Programming Tip: A member of the structure cannot be accessed directly using its
name. Rather you must use the structure name followed by the dot operator before
specifying the member name.
8.1.5 Copying and Comparing Structures
We can assign a structure to another structure of the same type using the assignment operator.
For example, if we have two structure variables stud1 and stud2 of type struct student:
struct student stud1 = {01, "Rahul", "BCA", 45000};
struct student stud2;
stud2 = stud1;
This statement initializes the members of stud2 with the values of members of stud1.
C does not permit comparison of one structure variable with another.
However, individual members of one structure can be compared with individual members of
another structure.
When we compare one structure member with another structure's member, the comparison
will behave like any other ordinary variable comparison. For example:
if ([Link] > [Link])
Programming Tip: An error will be generated if you try to compare two structure
variables. It is also an error to assign a structure of one type to a structure of another
type.
8.2 Arrays of Structures
In a class, we do not have just one student, but there may be at least 30 students.
So the same definition of the structure can be used for all the 30 students by creating an array of
the structure.
An array of a structure is declared in the same way as we declare an array of a built-in data type.
Another example is an organization that has a number of employees; defining a separate
structure for every employee is not a viable solution, so a common structure definition is used
by declaring an array of the structure employee.
The general syntax for declaring an array of a structure can be given as:
struct struct_name struct_var[index];
Consider the given structure definition and its array declaration:
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
struct student stud[30];
Now, to assign values to the ith student of the class, we will write:
stud[i].r_no = 09;
stud[i].name = "RASHI";
stud[i].course = "MCA";
stud[i].fees = 60000;
In order to initialize the array of structure variables at the time of declaration, you should write as
follows:
struct student stud[3] = {
{01, "Aman", "BCA", 45000},
{02, "Aryan", "MCA", 60000},
{03, "John", "BCA", 45000}
};