SlideShare a Scribd company logo
Structures
• A structure is a collection of related elements grouped together under a single name.
• Elements in a structure can be of same or different types.
• A smallest element in a structure is called a field.
• The difference between an array and a structure is that all elements in an array must be of
same type, while the elements in a structure can be of the same or different types.
Example: Structure
• C has two ways to declare a structure:
1) Tagged Structure
2) Type-defined Structures
Tagged Structure Example
• A tagged structure starts with a keyword struct.
• The second element in the declaration is the tag, an identifier for the structure.
• If the structure is concluded with a semicolon after the closing brace, no variables are
defined the structure is simply a type template with no associated storage.
Type Declaration with type-def
• A powerful way to declare a structure
• Format of typedef
• Type-defined structure differs from the tagged structure in two ways.
1. The keyword, typedef, is added to the beginning of the definition.
2. An identifier is required at the end of the block, the identifier is the type definition name
Declaring and Initialization
• Rules for structure initialization are similar to the rules for array intialization
• The initializers are enclosed in braces and separated by commas.
• They must match their corresponding types in the structure definition.
• When one or more intializers are missing, the structure elements will be assigned null
values, zero for integers and floating-point numbers, and null(‘0’) for characters and
strings.
Example: Initializing structures
Referencing Individual Fields
• Structure field can be accessed similar to an individual variable
• A field in a structure can be referred by referring to both the structure and the field.
Syntax
• To refer to a field in a structure, use the structure variable-identifier and then the field
identifier. The structure variable-identifier is separated from the field identifier by a dot.
Example: Structure Direct Selection operator
Defining Arrays for Structure
• Structures can have one or more arrays as members
• Arrays can be accessed either through indexing or through pointers
• They are properly qualified with direct selection operator
Example: Arrays in structures
• The student name and midterm stores are declared as arrays
• Initialization student s={“john”,{92,80,70},87};
Array of structures
• An array of structures needs to be created in many situations.
• Examples: Array of structure
• Use of an array of students to work with a group of students stored in a structure.
• Declare group of students as array
• Declare each student details as structure
• A structure declaration using arrays:
struct student
{
char name[15];
introllno;
char gender;
};
student stuAry[50];
Initialization
• An array of structures can be initialized in the same way as arrays in C
Example: Initializing an array of structures:
struct student
{
char name[15];
introllno;
char gender;
};
student stuAry[3]={ { “ram”,1234 , ‘m’}
{ “ravi”,2345 , ‘m’}
{ “sasi”,3456, ‘f’ }
};
• The above initialized values will be assigned by compiler as
stuAry[0].name=ram stuary[0].rollno=1234 stuary[0].gender=m
stuAry[1].name=ravi stuary[1].rollno=2345 stuary[1].gender=m
stuAry[2].name=sasi stuary[2].rollno=3456 stuary[2].gender=f
Unions
• The Union is a construct that allows memory to be shared by different types of data.
• A union is like a struct, but only one of its members is stored, not all
• I.e., a single variable may hold different types at different times
• Storage is enough to hold largest member
• Members are overlaid on top of each other
• Syntax: union union_name { <data-type> element 1;
<data-type> element 2;
<data-type> element 3; }union_variable;
• It is programmer’s responsibility to keep track of which type is stored in a union at any
given time!
• Example
structtaggedItem {
enum {iType, fType, cType} tag;
union {
intival;
float fval;
char *sval;
} u;
};
Unions are used much less frequently than structs — mostly
• in the inner details of operating system
• in device drivers
• in embedded systems where you have to access registers defined by the
hardware
Storage
• Size of union is the size of its largest member
• Avoid unions with widely varying member sizes
• for the larger data types, consider using pointers instead
Referencing Unions
• Rules for referencing a Union are identical to those for structure
• Direct Selection (dot) operator is used to reference individual fields within the
union
• When a union is referenced through pointer, the selection operator (arrow) can be
used
Example
ShareData.num
ShareData.chAry[0]
Initialization
• Union may only be initialized to a value appropriate for the type of its first
member
• The other types can only be initialized by assigning values or reading values into
the union.
• Enclose the values in a set of broces, even if there is only one value, when
initializing the union

More Related Content

What's hot (9)

PPTX
Dsa unit 1
ColorfullMedia
 
PPTX
Types in .net
Shivam Lohiya
 
PDF
Extracts from "Clean Code"
VlatkaPavii
 
PDF
Extracts from "Clean Code"
VlatkaPavii
 
DOCX
Io stream
mallica19
 
PPT
150950107056 2150704
Prashant Mokani
 
PDF
Jessup Moot Legal Research Guide 2017
La Trobe University Library- College of ASSC
 
PPTX
Elasticsearch: Removal of types
Taimur Qureshi
 
PDF
Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...
stepheneisenhauer
 
Dsa unit 1
ColorfullMedia
 
Types in .net
Shivam Lohiya
 
Extracts from "Clean Code"
VlatkaPavii
 
Extracts from "Clean Code"
VlatkaPavii
 
Io stream
mallica19
 
150950107056 2150704
Prashant Mokani
 
Jessup Moot Legal Research Guide 2017
La Trobe University Library- College of ASSC
 
Elasticsearch: Removal of types
Taimur Qureshi
 
Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...
stepheneisenhauer
 

Similar to Structures unions (20)

PPTX
C Programming: Structure and Union
Selvaraj Seerangan
 
PPT
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
PPTX
Structures
DrJasmineBeulahG
 
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPT
structure and union from C programming Language
AvrajeetGhosh
 
PPT
structure and union from c programming language.ppt
AvrajeetGhosh
 
PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
mahmoud2005rare
 
PPTX
Structure&amp;union
PralhadKhanal1
 
PPT
C Structures & Unions
Ram Sagar Mourya
 
DOCX
C programming structures &amp; union
Bathshebaparimala
 
PPT
structures and unions in 'C'
illpa
 
PPTX
Structure & union
lalithambiga kamaraj
 
PPTX
Module 5-Structure and Union
nikshaikh786
 
PDF
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
PDF
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
JNTUK KAKINADA
 
PPTX
User defined data types.pptx
Ananthi Palanisamy
 
DOC
Unit 5 (1)
psaravanan1985
 
PPT
SPC Unit 5
SIMONTHOMAS S
 
PPTX
Javaadvance applet and applet life cycle.pptx
sanjutoppo93
 
PPTX
structenumtypedefunion.pptx
KUPPALAPADMINI
 
C Programming: Structure and Union
Selvaraj Seerangan
 
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
Structures
DrJasmineBeulahG
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
structure and union from C programming Language
AvrajeetGhosh
 
structure and union from c programming language.ppt
AvrajeetGhosh
 
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
mahmoud2005rare
 
Structure&amp;union
PralhadKhanal1
 
C Structures & Unions
Ram Sagar Mourya
 
C programming structures &amp; union
Bathshebaparimala
 
structures and unions in 'C'
illpa
 
Structure & union
lalithambiga kamaraj
 
Module 5-Structure and Union
nikshaikh786
 
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
JNTUK KAKINADA
 
User defined data types.pptx
Ananthi Palanisamy
 
Unit 5 (1)
psaravanan1985
 
SPC Unit 5
SIMONTHOMAS S
 
Javaadvance applet and applet life cycle.pptx
sanjutoppo93
 
structenumtypedefunion.pptx
KUPPALAPADMINI
 
Ad

More from Sugnan M (7)

PPT
Python list 28_10_2020
Sugnan M
 
PPT
Python files 11_12_2020
Sugnan M
 
PPT
Python dictionary 02_11_2020
Sugnan M
 
PPT
Python command line_14_12_2020
Sugnan M
 
PPT
Multi threading 04 1_2021
Sugnan M
 
PPT
Exceptions 14 12_2020
Sugnan M
 
PPT
Modules 17 12_2020
Sugnan M
 
Python list 28_10_2020
Sugnan M
 
Python files 11_12_2020
Sugnan M
 
Python dictionary 02_11_2020
Sugnan M
 
Python command line_14_12_2020
Sugnan M
 
Multi threading 04 1_2021
Sugnan M
 
Exceptions 14 12_2020
Sugnan M
 
Modules 17 12_2020
Sugnan M
 
Ad

Recently uploaded (20)

PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
Inventory management chapter in automation and robotics.
atisht0104
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 

Structures unions

  • 1. Structures • A structure is a collection of related elements grouped together under a single name. • Elements in a structure can be of same or different types. • A smallest element in a structure is called a field. • The difference between an array and a structure is that all elements in an array must be of same type, while the elements in a structure can be of the same or different types. Example: Structure • C has two ways to declare a structure: 1) Tagged Structure 2) Type-defined Structures Tagged Structure Example • A tagged structure starts with a keyword struct. • The second element in the declaration is the tag, an identifier for the structure. • If the structure is concluded with a semicolon after the closing brace, no variables are defined the structure is simply a type template with no associated storage. Type Declaration with type-def • A powerful way to declare a structure • Format of typedef • Type-defined structure differs from the tagged structure in two ways. 1. The keyword, typedef, is added to the beginning of the definition. 2. An identifier is required at the end of the block, the identifier is the type definition name
  • 2. Declaring and Initialization • Rules for structure initialization are similar to the rules for array intialization • The initializers are enclosed in braces and separated by commas. • They must match their corresponding types in the structure definition. • When one or more intializers are missing, the structure elements will be assigned null values, zero for integers and floating-point numbers, and null(‘0’) for characters and strings. Example: Initializing structures Referencing Individual Fields • Structure field can be accessed similar to an individual variable • A field in a structure can be referred by referring to both the structure and the field. Syntax • To refer to a field in a structure, use the structure variable-identifier and then the field identifier. The structure variable-identifier is separated from the field identifier by a dot. Example: Structure Direct Selection operator Defining Arrays for Structure • Structures can have one or more arrays as members • Arrays can be accessed either through indexing or through pointers
  • 3. • They are properly qualified with direct selection operator Example: Arrays in structures • The student name and midterm stores are declared as arrays • Initialization student s={“john”,{92,80,70},87}; Array of structures • An array of structures needs to be created in many situations. • Examples: Array of structure • Use of an array of students to work with a group of students stored in a structure. • Declare group of students as array • Declare each student details as structure • A structure declaration using arrays: struct student { char name[15]; introllno; char gender; }; student stuAry[50]; Initialization • An array of structures can be initialized in the same way as arrays in C Example: Initializing an array of structures: struct student { char name[15]; introllno; char gender; }; student stuAry[3]={ { “ram”,1234 , ‘m’} { “ravi”,2345 , ‘m’} { “sasi”,3456, ‘f’ } }; • The above initialized values will be assigned by compiler as stuAry[0].name=ram stuary[0].rollno=1234 stuary[0].gender=m stuAry[1].name=ravi stuary[1].rollno=2345 stuary[1].gender=m stuAry[2].name=sasi stuary[2].rollno=3456 stuary[2].gender=f Unions • The Union is a construct that allows memory to be shared by different types of data.
  • 4. • A union is like a struct, but only one of its members is stored, not all • I.e., a single variable may hold different types at different times • Storage is enough to hold largest member • Members are overlaid on top of each other • Syntax: union union_name { <data-type> element 1; <data-type> element 2; <data-type> element 3; }union_variable; • It is programmer’s responsibility to keep track of which type is stored in a union at any given time! • Example structtaggedItem { enum {iType, fType, cType} tag; union { intival; float fval; char *sval; } u; }; Unions are used much less frequently than structs — mostly • in the inner details of operating system • in device drivers • in embedded systems where you have to access registers defined by the hardware Storage • Size of union is the size of its largest member • Avoid unions with widely varying member sizes • for the larger data types, consider using pointers instead Referencing Unions • Rules for referencing a Union are identical to those for structure • Direct Selection (dot) operator is used to reference individual fields within the union
  • 5. • When a union is referenced through pointer, the selection operator (arrow) can be used Example ShareData.num ShareData.chAry[0] Initialization • Union may only be initialized to a value appropriate for the type of its first member • The other types can only be initialized by assigning values or reading values into the union. • Enclose the values in a set of broces, even if there is only one value, when initializing the union