SlideShare una empresa de Scribd logo
1 de 10
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente (20)

Structure in c
Structure in cStructure in c
Structure in c
 
Enums in c
Enums in cEnums in c
Enums in c
 
structure and union
structure and unionstructure and union
structure and union
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
C functions
C functionsC functions
C functions
 
Typedef
TypedefTypedef
Typedef
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Unions in c
Unions in cUnions in c
Unions in c
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
C union
C unionC union
C union
 
Structure in c
Structure in cStructure in c
Structure in c
 
Pointers
PointersPointers
Pointers
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 

Destacado

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
 
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
 
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
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppteShikshak
 

Destacado (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
 
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
 
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
 
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’
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 

Similar 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
 

Similar 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
 

Más 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 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
 
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
 
Language processors
Language processorsLanguage processors
Language processorseShikshak
 

Más de eShikshak (19)

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 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
 
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
 
Language processors
Language processorsLanguage processors
Language processors
 

Último

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
 
Jumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3AJumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3Ajumarkdiezmo1
 
Back on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveBack on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveMarharyta Nedzelska
 
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
 
Training for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptTraining for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptVidalMendoza5
 
Spanish Classes Online In India With Tutor At Affordable Price
Spanish Classes Online In India With Tutor At Affordable PriceSpanish Classes Online In India With Tutor At Affordable Price
Spanish Classes Online In India With Tutor At Affordable PriceFluent Fast Academy
 
Nathan_Baughman_Resume_copywriter_and_editor
Nathan_Baughman_Resume_copywriter_and_editorNathan_Baughman_Resume_copywriter_and_editor
Nathan_Baughman_Resume_copywriter_and_editorNathanBaughman3
 
Career-Orientation-for-Grade-9-and-10.pptx
Career-Orientation-for-Grade-9-and-10.pptxCareer-Orientation-for-Grade-9-and-10.pptx
Career-Orientation-for-Grade-9-and-10.pptxGachaFluffy
 
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
 
4. Biomechanical Preparation INTRO AND TECHNIQUES
4. Biomechanical Preparation INTRO AND TECHNIQUES4. Biomechanical Preparation INTRO AND TECHNIQUES
4. Biomechanical Preparation INTRO AND TECHNIQUESaishwaryakhare5
 
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
 
Human Rights are notes and helping material
Human Rights are notes and helping materialHuman Rights are notes and helping material
Human Rights are notes and helping materialnadeemcollege26
 
LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024Bruce Bennett
 
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
 
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作rpb5qxou
 
How to prepare yourself for a job interview.pptx
How to prepare yourself for a job interview.pptxHow to prepare yourself for a job interview.pptx
How to prepare yourself for a job interview.pptxJohnreyFalsarioBasid
 
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
 
Abanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoubGhobrial1
 
Transportation and Trade Part 5 (1) (1).pptx
Transportation and Trade Part 5 (1) (1).pptxTransportation and Trade Part 5 (1) (1).pptx
Transportation and Trade Part 5 (1) (1).pptxSheldon Byron
 

Último (20)

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
 
Jumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3AJumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3A
 
Back on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveBack on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental Leave
 
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
 
Training for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptTraining for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.ppt
 
Spanish Classes Online In India With Tutor At Affordable Price
Spanish Classes Online In India With Tutor At Affordable PriceSpanish Classes Online In India With Tutor At Affordable Price
Spanish Classes Online In India With Tutor At Affordable Price
 
Nathan_Baughman_Resume_copywriter_and_editor
Nathan_Baughman_Resume_copywriter_and_editorNathan_Baughman_Resume_copywriter_and_editor
Nathan_Baughman_Resume_copywriter_and_editor
 
Career-Orientation-for-Grade-9-and-10.pptx
Career-Orientation-for-Grade-9-and-10.pptxCareer-Orientation-for-Grade-9-and-10.pptx
Career-Orientation-for-Grade-9-and-10.pptx
 
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...
 
4. Biomechanical Preparation INTRO AND TECHNIQUES
4. Biomechanical Preparation INTRO AND TECHNIQUES4. Biomechanical Preparation INTRO AND TECHNIQUES
4. Biomechanical Preparation INTRO AND TECHNIQUES
 
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
 
Human Rights are notes and helping material
Human Rights are notes and helping materialHuman Rights are notes and helping material
Human Rights are notes and helping material
 
LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024
 
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?
 
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
 
How to prepare yourself for a job interview.pptx
How to prepare yourself for a job interview.pptxHow to prepare yourself for a job interview.pptx
How to prepare yourself for a job interview.pptx
 
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
 
Abanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdf
 
Transportation and Trade Part 5 (1) (1).pptx
Transportation and Trade Part 5 (1) (1).pptxTransportation and Trade Part 5 (1) (1).pptx
Transportation and Trade Part 5 (1) (1).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