SlideShare a Scribd company logo
1 of 24
Department of FOT
By
Prof. Premavathi T
Unit - 6
Structure and union
01CE0101 - Computer
Programming
Structures
Structure
Arrays allow to define type of variables
that can hold several data items of the
same kind.
Similarly structure is another user
defined data type available in C that
allows to combine data items of different
kinds.
Structures are used to represent a record.
Defining a
Structure
 To define a structure, you must use
the struct statement.
 The struct statement defines a new data type,
with more than one member.
 The format of the struct statement is as follows
Example
Accessing
Structure
Members
To access any member of a structure, we use
the member access operator (.)
The member access operator is coded as a
period between the structure variable name
and the structure member that we wish to
access.
You would use the keyword struct to define
variables of structure type.
Example
Program
Example
Program
Program
output
Structures
as Function
Arguments
You can pass a structure as a function
argument in the same way as you pass any
other variable or pointer.
Union
Union
A union is a special data type available in
C that allows to store different data types
in the same memory location.
 You can define a union with many
members, but only one member can
contain a value at any given time.
 Unions provide an efficient way of using
the same memory location for multiple-
purpose.
Defining a
Union
To define a union, you must use
the union statement in the same way as
you did while defining a structure.
The union statement defines a new data
type with more than one member for
your program.
Defining a
Union
The format of the union statement is as
follows
Each member definition is a normal variable
definition, such as int i; or float f; or any
other valid variable definition.
At the end of the union's definition, before
the final semicolon, you can specify one or
more union variables but it is optional.
Defining a
Union
• Now, a variable of Data type can store an
integer, a floating-point number, or a string
of characters.
• It means a single variable, i.e., same
memory location, can be used to store
multiple types of data.
Example
Accessing
Union
Members
To access any member of a union, we
use the member access operator (.).
 The member access operator is coded
as a period between the union variable
name and the union member that we
wish to access.
You would use the keyword union to
define variables of union type.
Example
program
Difference
between
structure and
union
Advantages
of structure
• Structures gather more than one piece of data
about the same subject together in the same place.
• It is helpful when you want to gather the data of
similar data types and parameters like first name,
last name, etc.
• It is very easy to maintain as we can represent the
whole record by using a single name.
• In structure, we can pass complete set of records to
any function using a single parameter.
• You can use an array of structure to store more
records with similar types.
Disadvantag
es of
structure
 Change of one data structure in a code necessitates changes
at many other places.Therefore, the changes become hard
to track.
 Structure is slower because it requires storage space for all
the data.
 You can retrieve any member at a time in structure
whereas you can access one member at a time in the union.
 Structure occupies space for each and every member
written in inner parameters while union occupies space for a
member having the highest size written in inner parameters.
 Structure supports flexible array. Union does not support a
flexible array.
Advantages
of union
•It occupies less memory compared to
structure.
•When you use union, only the last variable
can be directly accessed.
•Union is used when you have to use the same
memory location for two or more data
members.
•It enables you to hold data of only one data
member.
•Its allocated space is equal to maximum size
of the data member.
Disadvantage
s of union
•You can use only one union
member at a time.
•All the union variables cannot
be initialized or used with
varying values at a time.
•Union assigns one common
storage space for all its
members.
That’s All….
Thank you!!!

More Related Content

Similar to Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx

Structures unions
Structures  unionsStructures  unions
Structures unionsSugnan M
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languagetanmaymodi4
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languageTanmay Modi
 
Structures in c++
Structures in c++Structures in c++
Structures in c++Swarup Boro
 
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...ssuser5610081
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppteShikshak
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and UnionSelvaraj Seerangan
 
object oriented analysis data.pptx
object oriented analysis data.pptxobject oriented analysis data.pptx
object oriented analysis data.pptxnibiganesh
 

Similar to Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx (20)

Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
 
Unit 3
Unit 3Unit 3
Unit 3
 
Programming in C
Programming in CProgramming in C
Programming in C
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
Structures unions
Structures  unionsStructures  unions
Structures unions
 
C- language Lecture 7
C- language Lecture 7C- language Lecture 7
C- language Lecture 7
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
UNION 1.pptx
UNION 1.pptxUNION 1.pptx
UNION 1.pptx
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
object oriented analysis data.pptx
object oriented analysis data.pptxobject oriented analysis data.pptx
object oriented analysis data.pptx
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Data models
Data modelsData models
Data models
 

Recently uploaded

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 

Recently uploaded (20)

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 

Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx

  • 1. Department of FOT By Prof. Premavathi T Unit - 6 Structure and union 01CE0101 - Computer Programming
  • 3. Structure Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds. Structures are used to represent a record.
  • 4. Defining a Structure  To define a structure, you must use the struct statement.  The struct statement defines a new data type, with more than one member.  The format of the struct statement is as follows
  • 6. Accessing Structure Members To access any member of a structure, we use the member access operator (.) The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type.
  • 10. Structures as Function Arguments You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
  • 11. Union
  • 12. Union A union is a special data type available in C that allows to store different data types in the same memory location.  You can define a union with many members, but only one member can contain a value at any given time.  Unions provide an efficient way of using the same memory location for multiple- purpose.
  • 13. Defining a Union To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program.
  • 14. Defining a Union The format of the union statement is as follows Each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the union's definition, before the final semicolon, you can specify one or more union variables but it is optional.
  • 15. Defining a Union • Now, a variable of Data type can store an integer, a floating-point number, or a string of characters. • It means a single variable, i.e., same memory location, can be used to store multiple types of data.
  • 17. Accessing Union Members To access any member of a union, we use the member access operator (.).  The member access operator is coded as a period between the union variable name and the union member that we wish to access. You would use the keyword union to define variables of union type.
  • 20. Advantages of structure • Structures gather more than one piece of data about the same subject together in the same place. • It is helpful when you want to gather the data of similar data types and parameters like first name, last name, etc. • It is very easy to maintain as we can represent the whole record by using a single name. • In structure, we can pass complete set of records to any function using a single parameter. • You can use an array of structure to store more records with similar types.
  • 21. Disadvantag es of structure  Change of one data structure in a code necessitates changes at many other places.Therefore, the changes become hard to track.  Structure is slower because it requires storage space for all the data.  You can retrieve any member at a time in structure whereas you can access one member at a time in the union.  Structure occupies space for each and every member written in inner parameters while union occupies space for a member having the highest size written in inner parameters.  Structure supports flexible array. Union does not support a flexible array.
  • 22. Advantages of union •It occupies less memory compared to structure. •When you use union, only the last variable can be directly accessed. •Union is used when you have to use the same memory location for two or more data members. •It enables you to hold data of only one data member. •Its allocated space is equal to maximum size of the data member.
  • 23. Disadvantage s of union •You can use only one union member at a time. •All the union variables cannot be initialized or used with varying values at a time. •Union assigns one common storage space for all its members.