Structures
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
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.
Structure
• Definition
• Declaration
• Accessing a structure members
• Structure initialization
Defining a Structure
• Syntax:
struct structure_name
{
data_type member_variable1;
data_type member_variable2;
………………………………;
data_type member_variableN;
};
Example 1
Defining a Structure for Student
struct student
{
char name[20];
int roll_no;
int m1,m2,m3;
int tot;
float avg;
};
Example 2
Defining a Structure for Employee
struct employee
{
char name[20];
int e_id;
int sal;
};
Example 3
Defining a Structure for Product
struct product
{
char prod_name[20];
int p_id;
int p_price;
};
Example 4
Defining a Structure for Time
struct time
{
int hour;
int min;
int sec;
};
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
Answer
Defining a Structure for Book
struct book
{
char b_name[50];
char ssn_no[50];
int price;
int quanity;
};
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;
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];
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];
Declaring structure variable
Example (time Structure Definition)
struct time
{
int hour;
int min;
int sec;
};
time Structure Variable Declaration
struct time t1;
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;
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
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
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
Question
Definition of a Structure for Time Given
Create variable for time and access the
members
struct time
{
int hour;
int min;
int sec;
};
Answer
Time Structure Variable Declaration
struct time t1;
Time Structure Accessing members
t1.hour
t1.min
t1.sec
Program to Read and Display Student
information
}Structure Definition
}Structure Declaration
}Structure Accessing Members
Student Total and Average
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.
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.
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.
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

Structures

  • 1.
  • 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… • Astructure 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.
  • 4.
    Structure • Definition • Declaration •Accessing a structure members • Structure initialization
  • 5.
    Defining a Structure •Syntax: struct structure_name { data_type member_variable1; data_type member_variable2; ………………………………; data_type member_variableN; };
  • 6.
    Example 1 Defining aStructure for Student struct student { char name[20]; int roll_no; int m1,m2,m3; int tot; float avg; };
  • 7.
    Example 2 Defining aStructure for Employee struct employee { char name[20]; int e_id; int sal; };
  • 8.
    Example 3 Defining aStructure for Product struct product { char prod_name[20]; int p_id; int p_price; };
  • 9.
    Example 4 Defining aStructure for Time struct time { int hour; int min; int sec; };
  • 10.
    Question Defining a Structurefor 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 Structurefor 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 structuremembers • 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 structuremembers 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 structuremembers 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 structuremembers 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 aStructure for Time Given Create variable for time and access the members struct time { int hour; int min; int sec; };
  • 21.
    Answer Time Structure VariableDeclaration struct time t1; Time Structure Accessing members t1.hour t1.min t1.sec
  • 22.
    Program to Readand Display Student information }Structure Definition }Structure Declaration }Structure Accessing Members
  • 23.
  • 24.
    Program Define a structureto 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.
  • 26.
    Program Define a structureto 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.
  • 28.
    Difference between Arrayand 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 Structureand 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