1. DISCOVER . LEARN . EMPOWER
Structure: Nested Structures, Array of
Structures
INSTITUTE - UIE
DEPARTMENT- ACADEMIC UNIT-2
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Fundamentals of Computer Programming
Subject Code: 21CSH-101
2. Fundamentals of
Computer
Programming
Course Objectives
2
The course aims to provide exposure to problem-
solving through programming.
The course aims to raise the programming skills
of students via logic building capability.
With knowledge of C programming language,
students would be able to model real world
problems.
3. 3
• 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.
• Example:
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
Nested Structure
6. Ways of Nesting a Structure
• The structure can be nested in the following ways:
1.By separate structure
2.By Embedded structure
6
7. Nesting a Structure by 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;
};
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. 7
8. Nesting a Structure by 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;
8
9. Accessing Nested Structure
• We can access the member of the nested structure by
Outer_Structure.Nested_Structure.member as given below:
e1.doj.dd
e1.doj.mm
e1.doj.yyyy
9
10. Nesting Structure Example
#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
int main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
//printing first employee information
printf( "employee id : %dn", e1.id);
printf( "employee name : %sn", e1.name);
printf( "employee date of joining (dd/mm/yyyy) : %d
/%d/%dn", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
return 0;
}
10
11. Output:
employee id : 101
employee name : Sonoo Jaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014
11
12. Array of Structures
• Declaring an array of structure is same as declaring an array of fundamental types.
• Since an array is a collection of elements of the same type.
• In an array of structures, each element of an array is of the structure type.
• Let's take an example:
struct car
{
char make[20];
char model[30];
int year;
};
12
13. Declaration of Array of Structures
Example: struct car arr_car[10];
• Here arr_car is an array of 10 elements where each
element is of type struct car.
• We can use arr_car to store 10 structure variables of
type struct car.
• To access individual elements we will use subscript
notation ([]) and to access the members of each
element we will use dot (.) operator as usual.
13
14. • arr_stu[0] : points to the 0th element of the array.
• arr_stu[1] : points to the 1st element of the array.
• arr_stu[0].name : refers to the name member of the 0th element of the array.
• arr_stu[0].roll_no : refers to the roll_no member of the 0th element of the array.
• arr_stu[0].marks : refers to the marks member of the 0th element of the array.
Note: the precedence of [] array subscript and dot(.) operator is same and they
evaluates from left to right. Therefore in the above expression first array
subscript([]) is applied followed by dot (.) operator. The array subscript ([]) and
dot(.) operator is same and they evaluates from left to right. Therefore in the above
expression first [] array subscript is applied followed by dot (.) operator.
14
15. Example:
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
// Create an array of structures
struct Point arr[10];
// Access array members
arr[0].x = 10;
arr[0].y = 20;
printf("%d %d", arr[0].x, arr[0].y);
return 0;
}
Output:
10 20
15
16. Structure Vs Union
Sr. No. Key Structure Union
1
Definition Structure is the container defined in C to store data variables
of different type and also supports for the user defined
variables storage.
On other hand Union is also similar kind of container in C
which can also holds the different type of variables along with
the user defined variables.
2
Internal implementation Structure in C is internally implemented as that there is
separate memory location is allotted to each input member
While in case Union memory is allocated only to one member
having largest size among all other input variables and the
same location is being get shared among all of these.
3
Syntax Syntax of declare a Structure in C is as follow :struct
struct_name{ type element1; type element2; . . } variable1,
variable2, ...;
On other syntax of declare a Union in C is as follow:union
u_name{ type element1; type element2; . . } variable1,
variable2, ...;
4
Size As mentioned in definition Structure do not have shared
location for its members so size of Structure is equal or greater
than the sum of size of all the data members.
On other hand Union does not have separate location for each
of its member so its size or equal to the size of largest member
among all data members.
5
Value storage As mentioned above in case of Structure there is specific
memory location for each input data member and hence it can
store multiple values of the different members.
While in case of Union there is only one shared memory
allocation for all input data members so it stores a single value
at a time for all members.
6
Initialization In Structure multiple members can be can be initializing at
same time.
On other hand in case of Union only the first member can get
initialize at a time.
16
17. 17
Summary
C provides us the feature of
nesting one structure within
another structure by using
which, complex data types are
created.
The structure can be nested in
the following ways.
a) By separate structure
b) By Embedded structure
We can access the member of
the nested structure by
Outer_Structure.Nested_Struct
ure.member
Declaring an array of structure
is same as declaring an array of
fundamental types.
In an array of structures, each
element of an array is of the
structure type.
We can also initialize the array
of structures using the same
syntax as that for initializing
arrays.
18. Frequently Asked Questions
Q1 What is the meaning of nested structure?
Ans: Nested structures as its name suggest in C is kind of defining one structure inside another structure. Any member variables can be
defined inside a structure and in turn, that structure can further be moved into another structure. The variables inside a structure can be
anything like normal or pointer or anything and can be placed anywhere within the structure
Q2 The correct syntax to access the member of the ith structure in the array of structures is?
struct temp
{
int b;
}s[50];
a) s.b.[i];
b) s.[i].b;
c) s.b[i];
d) s[i].b;
Ans: d
18
19. Q3: What will be the output of the C program?
void main()
{
struct bitfields {
int bits_1: 2;
int bits_2: 9;
int bits_3: 6;
int bits_4: 1;
}bit;
printf("%d", sizeof(bit));
}
A. 2
B. 3
C. 4
D. 0
Ans: Option: B
Explanation: 1 byte = 8 bits
In the above program we assign 2, 9, 6, 1 for the variables. Sum of the bits assigned is, 2 + 9 + 6 + 1
= 18, It is greater than 2 bytes, so it automatically takes 3 bytes.
19
20. Assessment Questions:
20
1. C Program to Calculate Size of Structure using Sizeof Operator
2. Write a C program to Sort Two Structures on the basis of any structure element and Display Information
Program Statement – Define a structure called cricket that will describe the following information
Player name
Team name
Batting average
Using cricket, declare an array player with 10 elements and write a program to read the information about all the 10
players and print a team wise list containing names of players with their batting average.
21. Discussion forum
PROBLEM: C Program to sort array of Structure
in C Programming
Write a C program to accept records of the different
states using array of structures. The structure should
contain char state, population, literacy rate, and
income. Display the state whose literacy rate is
highest and whose income is highest.
21
22. REFERENCES
Reference Books
1. Programming in C by Reema Thareja.
2. Programming in ANSI C by E. Balaguruswamy, Tata McGraw Hill.
3. Programming with C (Schaum's Outline Series) by Byron Gottfried Jitender
Chhabra, Tata McGraw Hill.
4. The C Programming Language by Brian W. Kernighan, Dennis Ritchie, Pearson
education.
Websites:
1. https://www.javatpoint.com/nested-structure-in-c
2. https://www.geeksforgeeks.org/structures-c/
3. https://overiq.com/c-programming-101/array-of-structures-in-c/
YouTube Links:
1. https://www.youtube.com/channel/UC63URkuUvnugRBeTNqmToKg
2. https://www.youtube.com/watch?v=0x9EVpv1V0k
3. https://www.youtube.com/watch?v=3LQTxwKZAOY 22