2. Introduction to Structure
Problem: – How to group together a collection
of data items of different types that are
logically related to a particular entity???
Example: Student Type which contains name,
rollno, marks of six subjects, total, average.
Solution: Structure
3. Structure contd…
• A structure is a collection of
variables of different data types
under a single name.
• The variables are called members of
the structure.
• The structure is also called a user-
defined data type.
6. Example 1
Defining a Structure for Student
struct student
{
char name[20];
int roll_no;
int m1,m2,m3;
int tot;
float avg;
};
7. Example 2
Defining a Structure for Employee
struct employee
{
char name[20];
int e_id;
int sal;
};
8. Example 3
Defining a Structure for Product
struct product
{
char prod_name[20];
int p_id;
int p_price;
};
9. Example 4
Defining a Structure for Time
struct time
{
int hour;
int min;
int sec;
};
10. Question
Defining a Structure for Book
• Create a Structure for book which contains
book name, book issn number, price of the
book, net quantity available
Requirement Data Type
book name String (array of
characters) size=50
book issn number String (array of
characters) size =10
price of the book int
net quantity
available
int
11. Answer
Defining a Structure for Book
struct book
{
char b_name[50];
char ssn_no[50];
int price;
int quanity;
};
12. Declaring structure variable
Syntax: struct structure_name structure_variable;
Example (Student Structure Definition)
struct student
{
char name[20];
int roll_no;
int m1,m2,m3;
int tot;
float avg;
};
Student Structure Variable Declaration
struct student s1;
13. Declaring structure variable
Example (employee Structure Definition)
struct employee
{
char name[20];
int e_id;
int sal;
};
Employee Structure Variable Declaration
struct employee e1,e2[10];
14. Declaring structure variable
Example (product Structure Definition)
struct product
{
char prod_name[20];
int p_id;
int p_price;
};
product Structure Variable Declaration
struct product p1[100];
15. Declaring structure variable
Example (time Structure Definition)
struct time
{
int hour;
int min;
int sec;
};
time Structure Variable Declaration
struct time t1;
16. Accessing a structure members
• Each variable of structure has its own copy of
member variables.
• The member variables are accessed using the
dot (.) operator or member operator.
• Syntax:
structure_variable.member;
17. Accessing a structure members
Syntax: structure_variable.member;
Example (Student Structure Definition)
struct student
{ char name[20];
int roll_no;
int m1,m2,m3;
int tot;
float avg; };
Student Structure Variable Declaration
struct student s1;
Student Structure Accessing members
s1. name s1. m1 s1. m3 s1. avg
s1. rollno s1. m2 s1. tot
18. Accessing a structure members
Example (employee Structure Definition)
struct employee{ char name[20];
int e_id;
int sal; };
Employee Structure Variable Declaration)
struct employee e1,e2[10];
Employee Structure Accessing members
e1. name e1. sal e1. e_id
Employee Structure Accessing members –Array
e2[0]. name e2[0]. sal e2[0]. e_id
e2[1]. name e2[1]. sal e2[1]. e_id
19. Accessing a structure members
Example (product Structure Definition)
struct product{char prod_name[20];
int p_id;
int p_price; };
product Structure Variable Declaration
struct product p1[100];
product Structure Accessing members (Array)
p1[0]. prod_name e2[0]. p_id e2[0]. p_price
p1[1]. prod_name e2[1]. p_id e2[1]. p_price
p1[2]. prod_name e2[2]. p_id e2[2]. p_price
20. Question
Definition of a Structure for Time Given
Create variable for time and access the
members
struct time
{
int hour;
int min;
int sec;
};
24. Program
Define a structure to store the student
details like name ,Roll no, marks in three
subjects, total and average. Read name
,Roll no, marks in three subjects fields and
write your logic to calculate the total and
average for n students and display rollno,
name, total and average.
25.
26. Program
Define a structure to store the employee
details like name, employee id, salary.
Read name , employee id, basic pay fields
and write your logic to calculate the
salary(bp+hra+da+pf) of a employee and
display employee id, name, and salary.
27.
28. Difference between Array and
Structure
Array Structure
Array is collection of homogeneous data. Structure is the collection of
heterogeneous data.
Array data are access using index. Structure elements are access using .
operator.
29. Difference between Structure and
Union
Union Structure
‘union’ is used to define a union ‘struct’ is used to describe a structure
Members share memory space Members share have individual memory
space
Size of a union is equal to that of the
largest member
The size of a structure is similar to the
total of the dimensions of all members