Lecture 3.2.2 Nested Structure Array of Structure
Lecture 3.2.2 Nested Structure Array of Structure
Course Objectives
CO5 Design and develop modular programs for real world problems us-
ing control structure and selection structure.
3
ASSESSMENT PATTERN
The performance of students is evaluated as follows:
Theory Practical
Continuous Internal As- Semester End Examina- Continuous Internal Semester End Exam-
Components sessment (CAE) tion (SEE) Assessment (CAE) ination (SEE)
Marks 40 60 60 40
Total Marks 100 100
4
Nested Structure
• 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];
};
5
struct employee
{
char name[20];
struct address add;
};
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
printf("Printing the employee information....\n");
printf("name: %s\nCity: %s\nPincode: %d\nPhone:%s“,emp.name,emp.add.city, emp.add.pin,
emp.add.phone);
}
6
Output:
Enter employee information?
Arun
Delhi
110001
1234567890
Printing the employee information....
name: Arun
City: Delhi
Pincode: 110001
Phone: 1234567890
7
Ways of Nesting a Structure
• The structure can be nested in the following ways:
8
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. 9
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;
10
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
11
Nesting Structure Example
#include <stdio.h> //storing employee information
#include <string.h> e1.id=101;
struct Employee strcpy(e1.name, "Sonoo Jaiswal
{ e1.doj.dd=10;
int id; e1.doj.mm=11;
char name[20]; e1.doj.yyyy=2014;
struct Date
{ //printing first employee information
int dd; printf( "employee id : %d\n", e1.id);
int mm; printf( "employee name : %s\n", e1.name);
int yyyy; printf( "employee date of joining (dd/mm/
}doj; yyyy) : %d/%d/%d\
}e1; n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
int main( ) return 0;
{ }
12
Output:
employee id : 101
employee name : Sonoo Jaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014
13
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;
};
14
Declaration of Array of
Structures
Example: struct car arr_car[10];
15
• 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.
16
Example:
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
// Create an array of structures
struct Point arr[10];
Internal Structure in C is internally implemented as that there is While in case Union memory is allocated only to one member
implementation separate memory location is allotted to each input member having largest size among all other input variables and the
2 same location is being get shared among all of these.
Syntax Syntax of declare a Structure in C is as follow :struct On other syntax of declare a Union in C is as follow:union
struct_name{ type element1; type element2; . . } variable1, u_name{ type element1; type element2; . . } variable1,
3 variable2, ...; variable2, ...;
Size As mentioned in definition Structure do not have shared On other hand Union does not have separate location for each
location for its members so size of Structure is equal or greater of its member so its size or equal to the size of largest member
4 than the sum of size of all the data members. among all data members.
Value storage As mentioned above in case of Structure there is specific While in case of Union there is only one shared memory
memory location for each input data member and hence it can allocation for all input data members so it stores a single value
5 store multiple values of the different members. at a time for all members.
Initialization In Structure multiple members can be can be initializing at On other hand in case of Union only the first member can get
6 same time. initialize at a time.
18
Summary
C provides us the feature of The structure can be nested in We can access the member of
nesting one structure within the following ways. the nested structure by
another structure by using
a) By separate structure Outer_Structure.Nested_Struct
which, complex data types are
b) By Embedded structure ure.member
created.
19
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
20
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.
21
Assessment Questions:
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.
22
Discussion forum
PROBLEM: C Program to sort array of Structure
in C Programming
23
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:
5. https://www.javatpoint.com/nested-structure-in-c
6. https://www.geeksforgeeks.org/structures-c/
7. https://overiq.com/c-programming-101/array-of-structures-in-c/
YouTube Links:
8. https://www.youtube.com/channel/UC63URkuUvnugRBeTNqmToKg
9. https://www.youtube.com/watch?v=0x9EVpv1V0k
10. https://www.youtube.com/watch?v=3LQTxwKZAOY 24
THANK YOU