SlideShare a Scribd company logo
Lectures on Busy Bee Workshop 1
This session Outline
Structure
Union
Difference between them
Structure & Union -C
Structure & Union -C
Lectures on Busy Bee Workshop 2
Data Types
In C programming language, divide the data into
different types. The various data types are
• Simple Data type (Fundamental)
 Integer, Real, Void, Char
• Structured Data type (Derived)
Array, Strings
• User Defined Data type (Programmer)
Enum, Structures, Unions
Structure and Union
Structure and Union
Lectures on Busy Bee Workshop 3
Structure Data Type
1. user defined data type
2. Groups logically related data items of different data
types into a single unit.
3. All the elements of a structure are stored at contiguous
memory locations.
4. A variable of structure type can store multiple data items of
different data types under the one name.
5. As the data of employee in company that is name,
Employee ID, salary, address, phone number is stored in
structure data type.
Structure
Structure
Lectures on Busy Bee Workshop 4
Structure
Structure
Defining of Structure
A structure has to defined, before it can used. The syntax of
defining a structure is
struct <struct_name>
{
<data_type> <variable_name>;
<data_type> <variable_name>;
……..
<data_type> <variable_name>;
};
Lectures on Busy Bee Workshop 5
Structure
Structure
Example of Structure
The structure of Employee is declared as
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Memory Space Allocation
8000
8002
8022
8024
8074
8076
employee
emp_id
name[20]
salary
address[50]
dept_no
age
Structure
Structure
Declaring a Structure Variable
A structure has to declared, after the body of structure
has defined. The syntax of declaring a structure is
struct <struct_name> <variable_name>;
The example to declare the variable for defined
structure “employee”
struct employee e1;
Here e1 variable contains 6 members that are defined
in structure.
Structure
Structure
Initializing a Structure Members
The members of individual structure variable is initialize
one by one or in a single statement. The example to
initialize a structure variable is
1)struct employee e1 = {1, “Suresh”,12000, “3 vikas
colony Madurai”,10, 35};
2)e1.emp_id=1; e1.dept_no=10;
e1.name=“Suresh”; e1.age=35;
e1.salary=12000;
e1.address=“3 vikas colony Madurai”;
Structure
Structure
Accessing a Structure Members
1. The structure members cannot be directly accessed in the
expression.
2. They are accessed by using the name of structure variable
followed by a dot and then the name of member
variable.
3. The method used to access the structure variables are
e1.emp_id, e1.name, e1.salary, e1.address, e1.dept_no,
e1.age.
4. The data with in the structure is stored and printed by this
method using scanf and printf statement in c program.
Structure
Structure
Structure Assignment
The value of one structure variable is assigned to
another variable of same type using assignment
statement. If the e1 and e2 are structure variables of
type employee then the statement
e1 = e2;
assign value of structure variable e2 to e1. The
value of each member of e2 is assigned to
corresponding members of e1.
Structure
Structure
Program to implement the Structure
Structure
Structure
Structure
Structure
Array of Structure
1. C allows to create an array of variables of
structure.
2. The array of structure is used to store the large
number of similar records.
3. For example to store the record of 100 employees
then array of structure is used.
4. This is similar to array.
5. The syntax to define the array of structure is
struct <struct_name> <var_name(array_name)>
[<value>];
For Example:-
struct employee e1[100];
Program to implement the Array of Structure
Structure
Structure
Structure
Structure
Structures within Structures
1. C define a variable of structure type as a member of other
structure type.
2. The syntax to define the structure within structure is
struct <struct_name>{
<data_type> <variable_name>;
struct <struct_name>
{ <data_type> <variable_name>;
……..}<struct_variable>;
<data_type> <variable_name>;
};
Structure
Structure
Example of Structure within Structure
The structure of Employee is declared as
struct employee
{
int emp_id;
char name[20];
float salary;
int dept_no;
struct date
{
int day;
int month;
int year;
}doj;
}e1;
Accessing Structures within Structures
For Example:-
e1.doj.day;
e1.doj.month;
e1.doj.year;
Pointers and Structures
The pointer variable to structure variable is declared as
struct employee *emp;
Access the Pointer in Structures
Access using arrow operator(->) instead of period operator(.).
For Example:
emp->name; emp->emp_id;
Structure
Structure
Passing Structure to Function
For Example:
printdata (struct employee e1); // Function calling
void printdata( struct employee emp) // Function defnition
{
…….
}
Function Returning Structure
struct employee getdata( )
{
….
return e1;
}
Structure
Structure
Union
Union
Union Data Type
1. A union is a user defined data type like structure.
2. The union groups logically related variables into a single
unit.
3. The union data type allocate the space equal to space
need to hold the largest data member of union.
4. The union allows different types of variable to share same
space in memory.
5. There is no other difference between structure and union
than internal difference.
6. The method to declare, use and access the union is same
as structure except the keyword union.
Union
Union
Example of Union
The union of Employee is declared as
union employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
}e;
Memory Space Allocation
8000
emp_id, dept_no, age
8002
salary
8004
name
8020
address
8050
Union
Union
Example Program of Union
Structure and Union (Self Study).pdfStructure and Union (Self Study).pdf
Structure and Union (Self Study).pdfStructure and Union (Self Study).pdf

More Related Content

PPT
structure and union
student
 
PPT
Structure and union
Samsil Arefin
 
PPT
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Rai University
 
PPT
pointer, structure ,union and intro to file handling
Rai University
 
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 
PPTX
Module 5-Structure and Union
nikshaikh786
 
DOC
Unit 5 (1)
psaravanan1985
 
PPT
Unit 5 structure and unions
kirthika jeyenth
 
structure and union
student
 
Structure and union
Samsil Arefin
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Rai University
 
pointer, structure ,union and intro to file handling
Rai University
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 
Module 5-Structure and Union
nikshaikh786
 
Unit 5 (1)
psaravanan1985
 
Unit 5 structure and unions
kirthika jeyenth
 

Similar to Structure and Union (Self Study).pdfStructure and Union (Self Study).pdf (20)

PPTX
Programming in C session 3
Prerna Sharma
 
PDF
C- language Lecture 7
Hatem Abd El-Salam
 
PDF
Principals of Programming in CModule -5.pdf
anilcsbs
 
PPTX
STRUCTURES IN C PROGRAMMING
Gurwinderkaur45
 
DOCX
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
Sitamarhi Institute of Technology
 
PPTX
Structures and Unions
Vijayananda Ratnam Ch
 
PPTX
Javaadvance applet and applet life cycle.pptx
sanjutoppo93
 
PPTX
Programming for problem solving-II(UNIT-2).pptx
prathima304
 
PPT
structure and union from C programming Language
AvrajeetGhosh
 
PPT
structure and union from c programming language.ppt
AvrajeetGhosh
 
PDF
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
PDF
VIT351 Software Development VI Unit4
YOGESH SINGH
 
PDF
Structure In C
yndaravind
 
PDF
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
JNTUK KAKINADA
 
PPTX
Data Structures and Algorithms_Updated.pptx
Kalpana Mohan
 
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
PPTX
C language.pptx
SANJUSANJEEVTOPPO
 
PPTX
Unit 9. Structure and Unions
Ashim Lamichhane
 
PPT
Structure in C
Fazle Rabbi Ador
 
DOCX
C programming structures &amp; union
Bathshebaparimala
 
Programming in C session 3
Prerna Sharma
 
C- language Lecture 7
Hatem Abd El-Salam
 
Principals of Programming in CModule -5.pdf
anilcsbs
 
STRUCTURES IN C PROGRAMMING
Gurwinderkaur45
 
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
Sitamarhi Institute of Technology
 
Structures and Unions
Vijayananda Ratnam Ch
 
Javaadvance applet and applet life cycle.pptx
sanjutoppo93
 
Programming for problem solving-II(UNIT-2).pptx
prathima304
 
structure and union from C programming Language
AvrajeetGhosh
 
structure and union from c programming language.ppt
AvrajeetGhosh
 
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
VIT351 Software Development VI Unit4
YOGESH SINGH
 
Structure In C
yndaravind
 
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
JNTUK KAKINADA
 
Data Structures and Algorithms_Updated.pptx
Kalpana Mohan
 
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
C language.pptx
SANJUSANJEEVTOPPO
 
Unit 9. Structure and Unions
Ashim Lamichhane
 
Structure in C
Fazle Rabbi Ador
 
C programming structures &amp; union
Bathshebaparimala
 
Ad

Recently uploaded (20)

PDF
Something I m waiting to tell you By Shravya Bhinder
patelprushti2007
 
PPTX
Joy And Peace In All Circumstances.pptx
FamilyWorshipCenterD
 
PDF
Exploring User Perspectives on Data Collection, Data Sharing Preferences, and...
Daniela Napoli
 
PPTX
Bob Stewart Journey to Rome 07 30 2025.pptx
FamilyWorshipCenterD
 
PPTX
Working-with-HTML-CSS-and-JavaScript.pptx
badalsenma5
 
PDF
Pesticides | Natural Pesticides | Methods of control | Types of pesticides | ...
Home
 
PPTX
Rotary_Fundraising_Overview_Updated_new video .pptx
allangraemeduncan
 
PDF
SXSW Panel Picker: Placemaking: Culture is the new cost of living
GabrielCohen28
 
PPTX
DPIC Assingment_1.pptx.pptx for presentation
yashwork2607
 
PDF
Securing Africa’s future: Technology, culture and the changing face of threat
Kayode Fayemi
 
PPTX
garment-industry in bangladesh. how bangladeshi industry is doing
tanvirhossain1570
 
PPTX
“Mastering Digital Professionalism: Your Online Image Matters”
ramjankhalyani
 
PPTX
GAMABA AWARDEES GINAW BILOG AND SALINTA MONON BY REYMART
purezagambala458
 
PPTX
Design Tips to Help Non-Visual Visitors Stay Safe Online
Daniela Napoli
 
DOCX
Ss Peter & Paul Choir Formation Training
kiambutownshipsecond
 
PDF
COSHH - Sri Ramachandar Bandi HSE in the Oil & Gas Industry (COSHH) Training ...
babufastdeals
 
PDF
Advanced-Web-Design-Crafting-the-Future-Web (1).pdf
vaghelavidhiba591
 
PPTX
Public Speakingbjdsbkjfdkjdasnlkdasnlknadslnbsjknsakjscbnkjbncs.pptx
ranazunairriaz1
 
PPTX
Raksha Bandhan Celebrations PPT festival
sowmyabapuram
 
PPTX
Mastering the DevOps Certification: CI/CD, Governance & Monitoring Made Simple
shubhamsharma994585
 
Something I m waiting to tell you By Shravya Bhinder
patelprushti2007
 
Joy And Peace In All Circumstances.pptx
FamilyWorshipCenterD
 
Exploring User Perspectives on Data Collection, Data Sharing Preferences, and...
Daniela Napoli
 
Bob Stewart Journey to Rome 07 30 2025.pptx
FamilyWorshipCenterD
 
Working-with-HTML-CSS-and-JavaScript.pptx
badalsenma5
 
Pesticides | Natural Pesticides | Methods of control | Types of pesticides | ...
Home
 
Rotary_Fundraising_Overview_Updated_new video .pptx
allangraemeduncan
 
SXSW Panel Picker: Placemaking: Culture is the new cost of living
GabrielCohen28
 
DPIC Assingment_1.pptx.pptx for presentation
yashwork2607
 
Securing Africa’s future: Technology, culture and the changing face of threat
Kayode Fayemi
 
garment-industry in bangladesh. how bangladeshi industry is doing
tanvirhossain1570
 
“Mastering Digital Professionalism: Your Online Image Matters”
ramjankhalyani
 
GAMABA AWARDEES GINAW BILOG AND SALINTA MONON BY REYMART
purezagambala458
 
Design Tips to Help Non-Visual Visitors Stay Safe Online
Daniela Napoli
 
Ss Peter & Paul Choir Formation Training
kiambutownshipsecond
 
COSHH - Sri Ramachandar Bandi HSE in the Oil & Gas Industry (COSHH) Training ...
babufastdeals
 
Advanced-Web-Design-Crafting-the-Future-Web (1).pdf
vaghelavidhiba591
 
Public Speakingbjdsbkjfdkjdasnlkdasnlknadslnbsjknsakjscbnkjbncs.pptx
ranazunairriaz1
 
Raksha Bandhan Celebrations PPT festival
sowmyabapuram
 
Mastering the DevOps Certification: CI/CD, Governance & Monitoring Made Simple
shubhamsharma994585
 
Ad

Structure and Union (Self Study).pdfStructure and Union (Self Study).pdf

  • 1. Lectures on Busy Bee Workshop 1 This session Outline Structure Union Difference between them Structure & Union -C Structure & Union -C
  • 2. Lectures on Busy Bee Workshop 2 Data Types In C programming language, divide the data into different types. The various data types are • Simple Data type (Fundamental)  Integer, Real, Void, Char • Structured Data type (Derived) Array, Strings • User Defined Data type (Programmer) Enum, Structures, Unions Structure and Union Structure and Union
  • 3. Lectures on Busy Bee Workshop 3 Structure Data Type 1. user defined data type 2. Groups logically related data items of different data types into a single unit. 3. All the elements of a structure are stored at contiguous memory locations. 4. A variable of structure type can store multiple data items of different data types under the one name. 5. As the data of employee in company that is name, Employee ID, salary, address, phone number is stored in structure data type. Structure Structure
  • 4. Lectures on Busy Bee Workshop 4 Structure Structure Defining of Structure A structure has to defined, before it can used. The syntax of defining a structure is struct <struct_name> { <data_type> <variable_name>; <data_type> <variable_name>; …….. <data_type> <variable_name>; };
  • 5. Lectures on Busy Bee Workshop 5 Structure Structure Example of Structure The structure of Employee is declared as struct employee { int emp_id; char name[20]; float salary; char address[50]; int dept_no; int age; };
  • 7. Declaring a Structure Variable A structure has to declared, after the body of structure has defined. The syntax of declaring a structure is struct <struct_name> <variable_name>; The example to declare the variable for defined structure “employee” struct employee e1; Here e1 variable contains 6 members that are defined in structure. Structure Structure
  • 8. Initializing a Structure Members The members of individual structure variable is initialize one by one or in a single statement. The example to initialize a structure variable is 1)struct employee e1 = {1, “Suresh”,12000, “3 vikas colony Madurai”,10, 35}; 2)e1.emp_id=1; e1.dept_no=10; e1.name=“Suresh”; e1.age=35; e1.salary=12000; e1.address=“3 vikas colony Madurai”; Structure Structure
  • 9. Accessing a Structure Members 1. The structure members cannot be directly accessed in the expression. 2. They are accessed by using the name of structure variable followed by a dot and then the name of member variable. 3. The method used to access the structure variables are e1.emp_id, e1.name, e1.salary, e1.address, e1.dept_no, e1.age. 4. The data with in the structure is stored and printed by this method using scanf and printf statement in c program. Structure Structure
  • 10. Structure Assignment The value of one structure variable is assigned to another variable of same type using assignment statement. If the e1 and e2 are structure variables of type employee then the statement e1 = e2; assign value of structure variable e2 to e1. The value of each member of e2 is assigned to corresponding members of e1. Structure Structure
  • 11. Program to implement the Structure Structure Structure
  • 12. Structure Structure Array of Structure 1. C allows to create an array of variables of structure. 2. The array of structure is used to store the large number of similar records. 3. For example to store the record of 100 employees then array of structure is used. 4. This is similar to array. 5. The syntax to define the array of structure is struct <struct_name> <var_name(array_name)> [<value>]; For Example:- struct employee e1[100];
  • 13. Program to implement the Array of Structure Structure Structure
  • 14. Structure Structure Structures within Structures 1. C define a variable of structure type as a member of other structure type. 2. The syntax to define the structure within structure is struct <struct_name>{ <data_type> <variable_name>; struct <struct_name> { <data_type> <variable_name>; ……..}<struct_variable>; <data_type> <variable_name>; };
  • 15. Structure Structure Example of Structure within Structure The structure of Employee is declared as struct employee { int emp_id; char name[20]; float salary; int dept_no; struct date { int day; int month; int year; }doj; }e1;
  • 16. Accessing Structures within Structures For Example:- e1.doj.day; e1.doj.month; e1.doj.year; Pointers and Structures The pointer variable to structure variable is declared as struct employee *emp; Access the Pointer in Structures Access using arrow operator(->) instead of period operator(.). For Example: emp->name; emp->emp_id; Structure Structure
  • 17. Passing Structure to Function For Example: printdata (struct employee e1); // Function calling void printdata( struct employee emp) // Function defnition { ……. } Function Returning Structure struct employee getdata( ) { …. return e1; } Structure Structure
  • 18. Union Union Union Data Type 1. A union is a user defined data type like structure. 2. The union groups logically related variables into a single unit. 3. The union data type allocate the space equal to space need to hold the largest data member of union. 4. The union allows different types of variable to share same space in memory. 5. There is no other difference between structure and union than internal difference. 6. The method to declare, use and access the union is same as structure except the keyword union.
  • 19. Union Union Example of Union The union of Employee is declared as union employee { int emp_id; char name[20]; float salary; char address[50]; int dept_no; int age; }e;
  • 20. Memory Space Allocation 8000 emp_id, dept_no, age 8002 salary 8004 name 8020 address 8050