C PDF
C PDF
Content:
1. C Structure
2. typedef in C
3. C Array of Structures
4. C Nested Structure
I. C Structure
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:
Construct individual arrays for storing names, roll numbers, and marks.
Use a special data structure to store the collection of different data
types.
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.
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
1
Let's see the example to define a structure for an entity employee in C:
struct employee
{ int id;
char name[20];
float salary;
};
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:
2
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:
By struct keyword within main() function
By declaring a variable at the time of defining the structure.
1st way:
Let's see the example to declare the structure variable by struct keyword. It
should be declared within the main function.
struct employee
{ int id;
char name[50];
float salary;
};
The variables e1 and e2 can be used to access the values stored in the
structure.
2nd way:
Let's see another way to declare variable at the time of defining the structure.
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
3
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:
By . (member or dot operator)
By -> (structure pointer operator)
Let's see the code to access the id member of p1 variable by. (member)
operator:
p1.id
4
Let's see another example of the structure in C language to store many
employees information.
5
II. typedef in C
The typedef is a keyword used in C programming to provide some meaningful
names to the already existing variable in the C program. It behaves similarly
as we define the alias for the commands. In short, we can say that this
keyword is used to redefine the name of an already existing variable.
Syntax of typedef
typedef <existing_name> <alias_name>
In the above statements, we have declared the unit variable of type unsigned
int by using a typedef keyword.
Now, we can create the variables of type unsigned int by writing the following
statement:
unit a, b;
instead of writing:
unsigned int a, b;
Till now, we have observed that the typedef keyword provides a nice shortcut
by providing an alternative name for an already existing variable. This
keyword is useful when we are dealing with the long data type especially,
structure declarations.
6
Let's understand through a simple example:
struct student
{
char name[20];
int age;
};
struct student s1;
7
The above statement shows the creation of a variable, i.e., s1, but the
statement is quite big. To avoid such a big statement, we use the typedef
keyword to create the variable of type student.
struct student
{
char name[20];
int age;
};
typedef struct student stud;
stud s1, s2;
In the above statement, we have declared the variable stud of type struct
student. Now, we can use the stud variable in a program to create the
variables of type struct student.
From the above declarations, we conclude that typedef keyword reduces the
length of the code and complexity of data types. It also helps in understanding
the program.
8
III. C Array of Structures
9
LAB: Create a program that create an array of structures that stores
information of 5 students and prints it.
10
Result/Output:
11
Example:
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.
struct Date
{
int dd;
int mm;
int yyyy;
};
12
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
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.
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.
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}emp1;
13
e1.doj.dd
e1.doj.mm
e1.doj.yyyy
14
15