SlideShare a Scribd company logo
-STRUCTURES
-UNIONS
STRUCTURES:
-A structure is a collection of variables of different types under a
single name.
-To define a structure, the struct keyword is used.
Syntax of structure:
struct structureName
{
dataType member1;
dataType member2;
...
};
Example:
struct Person
{
char name[50];
int citNo;
float salary;
};
Here, a derived type struct Person is defined.
Now, variables of this type can be created.
Creating structure variables:
-When a struct type is declared, no storage or memory is allocated.
-To allocate memory of a given structure type and work with it, we need to
create variables.
• Example:
struct Person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct Person person1, person2,
p[20];
………..
return 0;
}
struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
In both cases, two variables person1,
person2, and an array variable p
having 20 elements of type struct
Person are created.
Access members of a structure
. - Member operator
Suppose, you want to access the salary of
person2.
person2.salary
UNION:
-A union is a user-defined type similar to structs in C except for one key
difference.
-Struct allocate enough space to store all its members where as unions
allocate the space to store only the largest member.
EXAMPLE:
union car
{
char name[50];
int price;
};
The above code defines a derived type union car.
Creating union variables:
-When a union is defined, it creates a user-defined type.
-To allocate memory for a given union type and work with it, we need to create
variables.
union car
{
char name[50];
int price;
};
int main()
{
union car car1, car2;
return 0;
}
union car
{
char name[50];
int price;
} car1, car2;
In both cases, union variables car1,
car2, and a union pointer car3 of
union car type are created.
Access members of a union
-To access price for car1, car1.price is used.
DIFFERENCE BETWEEN
STRUCTURE AND UNION
union unionJob
{
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
• Size of union = 32
• Size of structure = 40
The size of a union variable will
always be the size of its largest
element.
In the given example, the size of its
largest element, (name[32]), is 32
bytes.
With a union, all members share the
same memory.

More Related Content

PPTX
Module 5-Structure and Union
nikshaikh786
 
DOCX
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
Sitamarhi Institute of Technology
 
PPT
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
PPTX
Unit 9. Structure and Unions
Ashim Lamichhane
 
PPTX
Structure in c language
sangrampatil81
 
PPTX
Programming in C session 3
Prerna Sharma
 
PDF
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
PPT
Unions
Shareb Ismaeel
 
Module 5-Structure and Union
nikshaikh786
 
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
Sitamarhi Institute of Technology
 
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
Unit 9. Structure and Unions
Ashim Lamichhane
 
Structure in c language
sangrampatil81
 
Programming in C session 3
Prerna Sharma
 
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
Unions
Shareb Ismaeel
 

Similar to STRUCTURES_UNION.pptx (20)

PPTX
Unit-V.pptx
Mehul Desai
 
DOCX
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Rajeshkumar Reddy
 
PPTX
Structures_Final_KLE (2).pptx
ShreevatsaAlawandi
 
PDF
03 structures
Rajan Gautam
 
PPTX
DS_Lecture_05.1_05.2.pptxnjnshsgxdhdgcdhcbdhc
nafizahmedsabed2
 
PPTX
structenumtypedefunion.pptx
KUPPALAPADMINI
 
PPTX
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
DeepasCSE
 
PDF
Unit 4 qba
Sowri Rajan
 
PPTX
Chapter 8 Structure Part 2 (1).pptx
Abhishekkumarsingh630054
 
PDF
C structure and union
Thesis Scientist Private Limited
 
PPTX
CPU : Structures And Unions
Dhrumil Patel
 
PPTX
C Structures and Unions
Dhrumil Patel
 
PPT
C Structures & Unions
Ram Sagar Mourya
 
DOC
Unit 5 (1)
psaravanan1985
 
PPT
Unit4 (2)
mrecedu
 
DOCX
Structure and Typedef
Acad
 
PDF
C- language Lecture 7
Hatem Abd El-Salam
 
PPTX
Unions.pptx
OluwafolakeOjo
 
PPTX
Structure & Union in C++
Davinder Kaur
 
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
Unit-V.pptx
Mehul Desai
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Rajeshkumar Reddy
 
Structures_Final_KLE (2).pptx
ShreevatsaAlawandi
 
03 structures
Rajan Gautam
 
DS_Lecture_05.1_05.2.pptxnjnshsgxdhdgcdhcbdhc
nafizahmedsabed2
 
structenumtypedefunion.pptx
KUPPALAPADMINI
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
DeepasCSE
 
Unit 4 qba
Sowri Rajan
 
Chapter 8 Structure Part 2 (1).pptx
Abhishekkumarsingh630054
 
C structure and union
Thesis Scientist Private Limited
 
CPU : Structures And Unions
Dhrumil Patel
 
C Structures and Unions
Dhrumil Patel
 
C Structures & Unions
Ram Sagar Mourya
 
Unit 5 (1)
psaravanan1985
 
Unit4 (2)
mrecedu
 
Structure and Typedef
Acad
 
C- language Lecture 7
Hatem Abd El-Salam
 
Unions.pptx
OluwafolakeOjo
 
Structure & Union in C++
Davinder Kaur
 
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
Ad

Recently uploaded (20)

PDF
The_Future_of_Data_Analytics_by_CA_Suvidha_Chaplot_UPDATED.pdf
CA Suvidha Chaplot
 
PPTX
World-population.pptx fire bunberbpeople
umutunsalnsl4402
 
PPT
Grade 5 PPT_Science_Q2_W6_Methods of reproduction.ppt
AaronBaluyut
 
PPTX
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
PDF
Company Presentation pada Perusahaan ADB.pdf
didikfahmi
 
PPTX
The whitetiger novel review for collegeassignment.pptx
DhruvPatel754154
 
PPTX
Introduction to Data Analytics and Data Science
KavithaCIT
 
PPTX
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
PPTX
Web dev -ppt that helps us understand web technology
shubhragoyal12
 
PPTX
Multiscale Segmentation of Survey Respondents: Seeing the Trees and the Fores...
Sione Palu
 
PPTX
Databricks-DE-Associate Certification Questions-june-2024.pptx
pedelli41
 
PPTX
Employee Salary Presentation.l based on data science collection of data
barridevakumari2004
 
PDF
TIC ACTIVIDAD 1geeeeeeeeeeeeeeeeeeeeeeeeeeeeeer3.pdf
Thais Ruiz
 
PPTX
Complete_STATA_Introduction_Beginner.pptx
mbayekebe
 
PDF
202501214233242351219 QASS Session 2.pdf
lauramejiamillan
 
PDF
Classifcation using Machine Learning and deep learning
bhaveshagrawal35
 
PPTX
short term project on AI Driven Data Analytics
JMJCollegeComputerde
 
PPTX
Data-Driven Machine Learning for Rail Infrastructure Health Monitoring
Sione Palu
 
PDF
Practical Measurement Systems Analysis (Gage R&R) for design
Rob Schubert
 
PDF
An Uncut Conversation With Grok | PDF Document
Mike Hydes
 
The_Future_of_Data_Analytics_by_CA_Suvidha_Chaplot_UPDATED.pdf
CA Suvidha Chaplot
 
World-population.pptx fire bunberbpeople
umutunsalnsl4402
 
Grade 5 PPT_Science_Q2_W6_Methods of reproduction.ppt
AaronBaluyut
 
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
Company Presentation pada Perusahaan ADB.pdf
didikfahmi
 
The whitetiger novel review for collegeassignment.pptx
DhruvPatel754154
 
Introduction to Data Analytics and Data Science
KavithaCIT
 
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
Web dev -ppt that helps us understand web technology
shubhragoyal12
 
Multiscale Segmentation of Survey Respondents: Seeing the Trees and the Fores...
Sione Palu
 
Databricks-DE-Associate Certification Questions-june-2024.pptx
pedelli41
 
Employee Salary Presentation.l based on data science collection of data
barridevakumari2004
 
TIC ACTIVIDAD 1geeeeeeeeeeeeeeeeeeeeeeeeeeeeeer3.pdf
Thais Ruiz
 
Complete_STATA_Introduction_Beginner.pptx
mbayekebe
 
202501214233242351219 QASS Session 2.pdf
lauramejiamillan
 
Classifcation using Machine Learning and deep learning
bhaveshagrawal35
 
short term project on AI Driven Data Analytics
JMJCollegeComputerde
 
Data-Driven Machine Learning for Rail Infrastructure Health Monitoring
Sione Palu
 
Practical Measurement Systems Analysis (Gage R&R) for design
Rob Schubert
 
An Uncut Conversation With Grok | PDF Document
Mike Hydes
 
Ad

STRUCTURES_UNION.pptx

  • 2. STRUCTURES: -A structure is a collection of variables of different types under a single name. -To define a structure, the struct keyword is used. Syntax of structure: struct structureName { dataType member1; dataType member2; ... };
  • 3. Example: struct Person { char name[50]; int citNo; float salary; }; Here, a derived type struct Person is defined. Now, variables of this type can be created.
  • 4. Creating structure variables: -When a struct type is declared, no storage or memory is allocated. -To allocate memory of a given structure type and work with it, we need to create variables. • Example: struct Person { char name[50]; int citNo; float salary; }; int main() { struct Person person1, person2, p[20]; ……….. return 0; } struct Person { char name[50]; int citNo; float salary; } person1, person2, p[20]; In both cases, two variables person1, person2, and an array variable p having 20 elements of type struct Person are created.
  • 5. Access members of a structure . - Member operator Suppose, you want to access the salary of person2. person2.salary
  • 6. UNION: -A union is a user-defined type similar to structs in C except for one key difference. -Struct allocate enough space to store all its members where as unions allocate the space to store only the largest member. EXAMPLE: union car { char name[50]; int price; }; The above code defines a derived type union car.
  • 7. Creating union variables: -When a union is defined, it creates a user-defined type. -To allocate memory for a given union type and work with it, we need to create variables. union car { char name[50]; int price; }; int main() { union car car1, car2; return 0; } union car { char name[50]; int price; } car1, car2; In both cases, union variables car1, car2, and a union pointer car3 of union car type are created.
  • 8. Access members of a union -To access price for car1, car1.price is used.
  • 9. DIFFERENCE BETWEEN STRUCTURE AND UNION union unionJob { char name[32]; float salary; int workerNo; } uJob; struct structJob { char name[32]; float salary; int workerNo; } sJob; • Size of union = 32 • Size of structure = 40 The size of a union variable will always be the size of its largest element. In the given example, the size of its largest element, (name[32]), is 32 bytes. With a union, all members share the same memory.