SlideShare uma empresa Scribd logo
1 de 10
Baixar para ler offline
Union




       Prakash Khaire, Lecturer
   B V Patel Inst. of BMC & IT, Gopal
               Vidyanagar




Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Define Union
●A union, is a collection of variables of different
types, just like a structure. However, with
unions, you can only store information in one
field at any one time
●You can picture a union as like a chunk of
memory that is used to store variables of
different types.
●Once a new value is assigned to a field, the
existing data is wiped over with the new data.



Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Union
●A union can also be viewed as a variable
type that can contain many different
variables (like a structure), but only actually
holds one of them at a time (not like a
structure).
●This can save memory if you have a group
of data where only one of the types is used
at a time.
●The size of a union is equal to the size of
it's largest data or element

       Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Union Syntax
union [union-type-name]
{
      type variable-names;
      type variable-names;
        …
        ...
}[union variable];



Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Union Example
union item
{
     int m;
     float x;
     char c;
} code;




Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Sharing of a storage locating by union
                     members

                      1000    1001     1002 1004



                        c

                         m

                                       x




Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Union Example
●The compiler will allocate enough storage in
a number to accommodate the largest
element in the union. Elements of a union
are accessed in the same manner as a
struct.
●Unlike a struct, the variables code.m, code.
x and code.c occupy the same location in
memory. Thus, writing into one will overwrite
the other.

Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Union
●During accessing, we should make sure that we
are accessing the member whose value is
currently stored.
code.m = 123;
code.x = 23.5;
printf(“%d”,code.m); //this will generate error
printf(“%d”,code.x);
●When a different member is assigned a new
value, the new value supersedes the pervious
member’s value.


Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Difference between Structure & Union
              Structure                                    Union
A structure allocates the total size of allA union only allocates as much memory as
elements in it.                            its largest element (member) requires.

Members inside a structure are alwaysThe union will store one and only one
stored in separate memory locationsactual value for one element at a time.
throughout the lifetime and scope of the
entire structure.
Manipulations of one member will notIf another element is stored before the
affect the values of any of the others infirst is retrieved, the first stored value is
any way unless they are operated on inlost.
code to do so.
The keyword struct is used to declare a The keyword union is used to declare an
structure                               union.
All data members in a structure are activeOnly on data member is active at a time.
at time



            Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Similarities between Structure & Union
They both can accept the dot (.) operator to address a member from the
object name, as struct.member or union.member


They both use brace delimited declarations to create the template for the
data object. Both accept tagname and name as well as explicit initialization
as options.




They both can have their size correctly determined as maximum size in
bytes by use of the sizeof() operator.




          Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Mais conteúdo relacionado

Mais procurados (20)

Data types in c++
Data types in c++Data types in c++
Data types in c++
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Unions in c
Unions in cUnions in c
Unions in c
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Pointers
PointersPointers
Pointers
 
String in c programming
String in c programmingString in c programming
String in c programming
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
structure and union
structure and unionstructure and union
structure and union
 
Function template
Function templateFunction template
Function template
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
String functions in C
String functions in CString functions in C
String functions in C
 
class and objects
class and objectsclass and objects
class and objects
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
C Tokens
C TokensC Tokens
C Tokens
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Friend function
Friend functionFriend function
Friend function
 

Destaque

Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloudeShikshak
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computingeShikshak
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computingeShikshak
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computingeShikshak
 
C programming structure & pointer
C  programming structure & pointerC  programming structure & pointer
C programming structure & pointerargusacademy
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and UnionsDhrumil Patel
 
Lecture 12 css_fonts
Lecture 12 css_fontsLecture 12 css_fonts
Lecture 12 css_fontseShikshak
 
Introduction to css
Introduction to cssIntroduction to css
Introduction to csseShikshak
 
Lecture 11 css_inculsion
Lecture 11 css_inculsionLecture 11 css_inculsion
Lecture 11 css_inculsioneShikshak
 
Introduction to computer_and_its_structure
Introduction to computer_and_its_structureIntroduction to computer_and_its_structure
Introduction to computer_and_its_structureeShikshak
 
Program development cyle
Program development cyleProgram development cyle
Program development cyleeShikshak
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppteShikshak
 
Computer programming programming_langugages
Computer programming programming_langugagesComputer programming programming_langugages
Computer programming programming_langugageseShikshak
 
Html phrase tags
Html phrase tagsHtml phrase tags
Html phrase tagseShikshak
 
Lecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.pptLecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.ppteShikshak
 

Destaque (20)

C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computing
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computing
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computing
 
C programming structure & pointer
C  programming structure & pointerC  programming structure & pointer
C programming structure & pointer
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Lecture 12 css_fonts
Lecture 12 css_fontsLecture 12 css_fonts
Lecture 12 css_fonts
 
Introduction to css
Introduction to cssIntroduction to css
Introduction to css
 
Lecture 11 css_inculsion
Lecture 11 css_inculsionLecture 11 css_inculsion
Lecture 11 css_inculsion
 
Introduction to computer_and_its_structure
Introduction to computer_and_its_structureIntroduction to computer_and_its_structure
Introduction to computer_and_its_structure
 
Program development cyle
Program development cyleProgram development cyle
Program development cyle
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Computer programming programming_langugages
Computer programming programming_langugagesComputer programming programming_langugages
Computer programming programming_langugages
 
Html phrase tags
Html phrase tagsHtml phrase tags
Html phrase tags
 
Lecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.pptLecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.ppt
 

Semelhante a Lecture19 unionsin c.ppt

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
 
Structures in c++
Structures in c++Structures in c++
Structures in c++Swarup Boro
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and UnionSelvaraj Seerangan
 
Neural Network in Knowledge Bases
Neural Network in Knowledge BasesNeural Network in Knowledge Bases
Neural Network in Knowledge BasesKushal Arora
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structuresKrishna Nanda
 
Classification of text data using feature clustering algorithm
Classification of text data using feature clustering algorithmClassification of text data using feature clustering algorithm
Classification of text data using feature clustering algorithmeSAT Publishing House
 
Structures
StructuresStructures
Structuresselvapon
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and oodthan sare
 
A hybrid constructive algorithm incorporating teaching-learning based optimiz...
A hybrid constructive algorithm incorporating teaching-learning based optimiz...A hybrid constructive algorithm incorporating teaching-learning based optimiz...
A hybrid constructive algorithm incorporating teaching-learning based optimiz...IJECEIAES
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .NetGreg Sohl
 

Semelhante a Lecture19 unionsin c.ppt (20)

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
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
Nn kb
Nn kbNn kb
Nn kb
 
Neural Network in Knowledge Bases
Neural Network in Knowledge BasesNeural Network in Knowledge Bases
Neural Network in Knowledge Bases
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Classification of text data using feature clustering algorithm
Classification of text data using feature clustering algorithmClassification of text data using feature clustering algorithm
Classification of text data using feature clustering algorithm
 
Unit 3
Unit 3Unit 3
Unit 3
 
Structures
StructuresStructures
Structures
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
UNIONS.pptx
UNIONS.pptxUNIONS.pptx
UNIONS.pptx
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
 
A hybrid constructive algorithm incorporating teaching-learning based optimiz...
A hybrid constructive algorithm incorporating teaching-learning based optimiz...A hybrid constructive algorithm incorporating teaching-learning based optimiz...
A hybrid constructive algorithm incorporating teaching-learning based optimiz...
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
Concepts of oops
Concepts of oopsConcepts of oops
Concepts of oops
 

Mais de eShikshak

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluationeShikshak
 
Operators in python
Operators in pythonOperators in python
Operators in pythoneShikshak
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythoneShikshak
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerceeShikshak
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computingeShikshak
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'eShikshak
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variableseShikshak
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorseShikshak
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppteShikshak
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppteShikshak
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppteShikshak
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppteShikshak
 
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02eShikshak
 

Mais de eShikshak (20)

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppt
 
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02
 

Último

Complete Benefits of career counseling in India
Complete Benefits of career counseling in IndiaComplete Benefits of career counseling in India
Complete Benefits of career counseling in IndiaMere Mentor
 
Senior IT Professional with Master’s Degree with 21+ years of experience is...
Senior IT Professional with Master’s Degree with 21+ years of experience   is...Senior IT Professional with Master’s Degree with 21+ years of experience   is...
Senior IT Professional with Master’s Degree with 21+ years of experience is...Anas Acharath Parakat
 
Thomas Calculus 12th Edition Textbook and helping material
Thomas Calculus 12th Edition Textbook and helping materialThomas Calculus 12th Edition Textbook and helping material
Thomas Calculus 12th Edition Textbook and helping materialsafdarhussainbhutta4
 
Bobby singh - Digital Marketing Service
Bobby singh -  Digital Marketing ServiceBobby singh -  Digital Marketing Service
Bobby singh - Digital Marketing ServiceBobby singh
 
About Kelly Wang: Generative AI, SEO, PR
About Kelly Wang: Generative AI, SEO, PRAbout Kelly Wang: Generative AI, SEO, PR
About Kelly Wang: Generative AI, SEO, PRKelly Wang
 
Youth Career and Employment Insights and informative Charts
Youth Career and Employment Insights and informative ChartsYouth Career and Employment Insights and informative Charts
Youth Career and Employment Insights and informative ChartsYouthLab
 
FACILITATING LEARNER-CENTERED TEACHING pptx
FACILITATING LEARNER-CENTERED TEACHING pptxFACILITATING LEARNER-CENTERED TEACHING pptx
FACILITATING LEARNER-CENTERED TEACHING pptxJAMESPARAQUINAN
 
Abanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoubGhobrial1
 
Design, Desire and Demand Presentation.pptx
Design, Desire and Demand Presentation.pptxDesign, Desire and Demand Presentation.pptx
Design, Desire and Demand Presentation.pptxaaronbasko1
 
The Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating PressThe Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating Pressmatingpress170
 
How to make career in advance 3d animation
How to make career in advance 3d animationHow to make career in advance 3d animation
How to make career in advance 3d animationsantoshjadhav126
 
What is the career path of a VFX artist?
What is the career path of a VFX artist?What is the career path of a VFX artist?
What is the career path of a VFX artist?santoshjadhav126
 
Get to know about Raquel Thompson Barbados.pdf
Get to know about Raquel Thompson Barbados.pdfGet to know about Raquel Thompson Barbados.pdf
Get to know about Raquel Thompson Barbados.pdfRaquel Thompson Barbados
 
Training for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptTraining for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptVidalMendoza5
 
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024Hector Del Castillo, CPM, CPMM
 
Kelly Wang 王薇盈_數位轉型、生成式AI、設計思考、客戶體驗優化、敏捷管理、SEO
Kelly Wang 王薇盈_數位轉型、生成式AI、設計思考、客戶體驗優化、敏捷管理、SEOKelly Wang 王薇盈_數位轉型、生成式AI、設計思考、客戶體驗優化、敏捷管理、SEO
Kelly Wang 王薇盈_數位轉型、生成式AI、設計思考、客戶體驗優化、敏捷管理、SEOKelly Wang
 
Banged Dubai Call Girls O525547819 Call Girls Dubai
Banged Dubai Call Girls O525547819 Call Girls DubaiBanged Dubai Call Girls O525547819 Call Girls Dubai
Banged Dubai Call Girls O525547819 Call Girls Dubaikojalkojal131
 
Network to Success - Using Social Media in Job Search
Network to Success - Using Social Media in Job SearchNetwork to Success - Using Social Media in Job Search
Network to Success - Using Social Media in Job SearchBruce Bennett
 
APSC Motor Vechile Inspector 18 Posts.pdf
APSC Motor Vechile Inspector 18 Posts.pdfAPSC Motor Vechile Inspector 18 Posts.pdf
APSC Motor Vechile Inspector 18 Posts.pdfsoumita869
 
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptxwaghmare9860lavin
 

Último (20)

Complete Benefits of career counseling in India
Complete Benefits of career counseling in IndiaComplete Benefits of career counseling in India
Complete Benefits of career counseling in India
 
Senior IT Professional with Master’s Degree with 21+ years of experience is...
Senior IT Professional with Master’s Degree with 21+ years of experience   is...Senior IT Professional with Master’s Degree with 21+ years of experience   is...
Senior IT Professional with Master’s Degree with 21+ years of experience is...
 
Thomas Calculus 12th Edition Textbook and helping material
Thomas Calculus 12th Edition Textbook and helping materialThomas Calculus 12th Edition Textbook and helping material
Thomas Calculus 12th Edition Textbook and helping material
 
Bobby singh - Digital Marketing Service
Bobby singh -  Digital Marketing ServiceBobby singh -  Digital Marketing Service
Bobby singh - Digital Marketing Service
 
About Kelly Wang: Generative AI, SEO, PR
About Kelly Wang: Generative AI, SEO, PRAbout Kelly Wang: Generative AI, SEO, PR
About Kelly Wang: Generative AI, SEO, PR
 
Youth Career and Employment Insights and informative Charts
Youth Career and Employment Insights and informative ChartsYouth Career and Employment Insights and informative Charts
Youth Career and Employment Insights and informative Charts
 
FACILITATING LEARNER-CENTERED TEACHING pptx
FACILITATING LEARNER-CENTERED TEACHING pptxFACILITATING LEARNER-CENTERED TEACHING pptx
FACILITATING LEARNER-CENTERED TEACHING pptx
 
Abanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdf
 
Design, Desire and Demand Presentation.pptx
Design, Desire and Demand Presentation.pptxDesign, Desire and Demand Presentation.pptx
Design, Desire and Demand Presentation.pptx
 
The Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating PressThe Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating Press
 
How to make career in advance 3d animation
How to make career in advance 3d animationHow to make career in advance 3d animation
How to make career in advance 3d animation
 
What is the career path of a VFX artist?
What is the career path of a VFX artist?What is the career path of a VFX artist?
What is the career path of a VFX artist?
 
Get to know about Raquel Thompson Barbados.pdf
Get to know about Raquel Thompson Barbados.pdfGet to know about Raquel Thompson Barbados.pdf
Get to know about Raquel Thompson Barbados.pdf
 
Training for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptTraining for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.ppt
 
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
 
Kelly Wang 王薇盈_數位轉型、生成式AI、設計思考、客戶體驗優化、敏捷管理、SEO
Kelly Wang 王薇盈_數位轉型、生成式AI、設計思考、客戶體驗優化、敏捷管理、SEOKelly Wang 王薇盈_數位轉型、生成式AI、設計思考、客戶體驗優化、敏捷管理、SEO
Kelly Wang 王薇盈_數位轉型、生成式AI、設計思考、客戶體驗優化、敏捷管理、SEO
 
Banged Dubai Call Girls O525547819 Call Girls Dubai
Banged Dubai Call Girls O525547819 Call Girls DubaiBanged Dubai Call Girls O525547819 Call Girls Dubai
Banged Dubai Call Girls O525547819 Call Girls Dubai
 
Network to Success - Using Social Media in Job Search
Network to Success - Using Social Media in Job SearchNetwork to Success - Using Social Media in Job Search
Network to Success - Using Social Media in Job Search
 
APSC Motor Vechile Inspector 18 Posts.pdf
APSC Motor Vechile Inspector 18 Posts.pdfAPSC Motor Vechile Inspector 18 Posts.pdf
APSC Motor Vechile Inspector 18 Posts.pdf
 
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
 

Lecture19 unionsin c.ppt

  • 1. Union Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 2. Define Union ●A union, is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time ●You can picture a union as like a chunk of memory that is used to store variables of different types. ●Once a new value is assigned to a field, the existing data is wiped over with the new data. Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 3. Union ●A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds one of them at a time (not like a structure). ●This can save memory if you have a group of data where only one of the types is used at a time. ●The size of a union is equal to the size of it's largest data or element Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 4. Union Syntax union [union-type-name] { type variable-names; type variable-names; … ... }[union variable]; Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 5. Union Example union item { int m; float x; char c; } code; Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 6. Sharing of a storage locating by union members 1000 1001 1002 1004 c m x Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 7. Union Example ●The compiler will allocate enough storage in a number to accommodate the largest element in the union. Elements of a union are accessed in the same manner as a struct. ●Unlike a struct, the variables code.m, code. x and code.c occupy the same location in memory. Thus, writing into one will overwrite the other. Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 8. Union ●During accessing, we should make sure that we are accessing the member whose value is currently stored. code.m = 123; code.x = 23.5; printf(“%d”,code.m); //this will generate error printf(“%d”,code.x); ●When a different member is assigned a new value, the new value supersedes the pervious member’s value. Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 9. Difference between Structure & Union Structure Union A structure allocates the total size of allA union only allocates as much memory as elements in it. its largest element (member) requires. Members inside a structure are alwaysThe union will store one and only one stored in separate memory locationsactual value for one element at a time. throughout the lifetime and scope of the entire structure. Manipulations of one member will notIf another element is stored before the affect the values of any of the others infirst is retrieved, the first stored value is any way unless they are operated on inlost. code to do so. The keyword struct is used to declare a The keyword union is used to declare an structure union. All data members in a structure are activeOnly on data member is active at a time. at time Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 10. Similarities between Structure & Union They both can accept the dot (.) operator to address a member from the object name, as struct.member or union.member They both use brace delimited declarations to create the template for the data object. Both accept tagname and name as well as explicit initialization as options. They both can have their size correctly determined as maximum size in bytes by use of the sizeof() operator. Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar