SlideShare a Scribd company logo
1 of 5
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

Extracts from "Clean Code"
Extracts from "Clean Code"Extracts from "Clean Code"
Extracts from "Clean Code"VlatkaPavii
 
Extracts from "Clean Code"
Extracts from "Clean Code"Extracts from "Clean Code"
Extracts from "Clean Code"VlatkaPavii
 
Elasticsearch: Removal of types
Elasticsearch: Removal of typesElasticsearch: Removal of types
Elasticsearch: Removal of typesTaimur Qureshi
 
Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...
Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...
Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...stepheneisenhauer
 

What's hot (9)

Dsa unit 1
Dsa unit 1Dsa unit 1
Dsa unit 1
 
Types in .net
Types in .netTypes in .net
Types in .net
 
Extracts from "Clean Code"
Extracts from "Clean Code"Extracts from "Clean Code"
Extracts from "Clean Code"
 
Extracts from "Clean Code"
Extracts from "Clean Code"Extracts from "Clean Code"
Extracts from "Clean Code"
 
Io stream
Io streamIo stream
Io stream
 
150950107056 2150704
150950107056 2150704150950107056 2150704
150950107056 2150704
 
Jessup Moot Legal Research Guide 2017
Jessup Moot Legal Research Guide 2017Jessup Moot Legal Research Guide 2017
Jessup Moot Legal Research Guide 2017
 
Elasticsearch: Removal of types
Elasticsearch: Removal of typesElasticsearch: Removal of types
Elasticsearch: Removal of types
 
Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...
Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...
Digging into File Formats: Poking around at data using file, DROID, JHOVE, an...
 

Similar to Structures unions

C++ training
C++ training C++ training
C++ training PL Sharma
 
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
 
Data structure chapter 1.pptx
Data structure chapter 1.pptxData structure chapter 1.pptx
Data structure chapter 1.pptxKami503928
 
Object database standards, languages and design
Object database standards, languages and designObject database standards, languages and design
Object database standards, languages and designDabbal Singh Mahara
 
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxUnit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxvekariyakashyap
 
CSharp for Unity Day 3
CSharp for Unity Day 3CSharp for Unity Day 3
CSharp for Unity Day 3Duong Thanh
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation ashu awais
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane2
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdfmarkbrianBautista
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and UnionsAshim Lamichhane
 

Similar to Structures unions (20)

SPC Unit 5
SPC Unit 5SPC Unit 5
SPC Unit 5
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Struct
StructStruct
Struct
 
C++ training
C++ training C++ training
C++ training
 
Apex code (Salesforce)
Apex code (Salesforce)Apex code (Salesforce)
Apex code (Salesforce)
 
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
 
Data structure chapter 1.pptx
Data structure chapter 1.pptxData structure chapter 1.pptx
Data structure chapter 1.pptx
 
Object database standards, languages and design
Object database standards, languages and designObject database standards, languages and design
Object database standards, languages and design
 
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxUnit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
 
java.pdf
java.pdfjava.pdf
java.pdf
 
Java
JavaJava
Java
 
CSharp for Unity Day 3
CSharp for Unity Day 3CSharp for Unity Day 3
CSharp for Unity Day 3
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Ooad ch 2
Ooad ch 2Ooad ch 2
Ooad ch 2
 
Structure
StructureStructure
Structure
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 

More from Sugnan M

Python list 28_10_2020
Python list 28_10_2020Python list 28_10_2020
Python list 28_10_2020Sugnan M
 
Python files 11_12_2020
Python files 11_12_2020Python files 11_12_2020
Python files 11_12_2020Sugnan M
 
Python dictionary 02_11_2020
Python dictionary 02_11_2020Python dictionary 02_11_2020
Python dictionary 02_11_2020Sugnan M
 
Python command line_14_12_2020
Python command line_14_12_2020Python command line_14_12_2020
Python command line_14_12_2020Sugnan M
 
Multi threading 04 1_2021
Multi threading 04 1_2021Multi threading 04 1_2021
Multi threading 04 1_2021Sugnan M
 
Exceptions 14 12_2020
Exceptions 14 12_2020Exceptions 14 12_2020
Exceptions 14 12_2020Sugnan M
 
Modules 17 12_2020
Modules 17 12_2020Modules 17 12_2020
Modules 17 12_2020Sugnan M
 

More from Sugnan M (7)

Python list 28_10_2020
Python list 28_10_2020Python list 28_10_2020
Python list 28_10_2020
 
Python files 11_12_2020
Python files 11_12_2020Python files 11_12_2020
Python files 11_12_2020
 
Python dictionary 02_11_2020
Python dictionary 02_11_2020Python dictionary 02_11_2020
Python dictionary 02_11_2020
 
Python command line_14_12_2020
Python command line_14_12_2020Python command line_14_12_2020
Python command line_14_12_2020
 
Multi threading 04 1_2021
Multi threading 04 1_2021Multi threading 04 1_2021
Multi threading 04 1_2021
 
Exceptions 14 12_2020
Exceptions 14 12_2020Exceptions 14 12_2020
Exceptions 14 12_2020
 
Modules 17 12_2020
Modules 17 12_2020Modules 17 12_2020
Modules 17 12_2020
 

Recently uploaded

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 

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