SlideShare a Scribd company logo
1 of 10
Another Way to Collect Dissimilar Data
What Is a Union?
• a block of memory that is used to hold data items of different
types.
– Declaring Unions & Defining Union Variables
union automobile {
int year;
char model[8];
int engine_power;
float weight;} sedan, pick_up, sport_utility;
– Referring a Union with . or ->
sedan.year = 1997;
union automobile *ptr; ptr->year = 1997;
Ex1:
/* 20L01.c Referencing a union */
#include <stdio.h>
#include <string.h>
main(void)
{
union menu {
char name[23];
double price;
} dish;
printf("The content assigned to the union separately:n");
/* reference name */
strcpy(dish.name, "Sweet and Sour Chicken");
printf("Dish Name: %sn", dish.name);
/* reference price */
dish.price = 9.95;
printf("Dish Price: %5.2fn", dish.price);
return 0;
}
Ex2:
#include <stdio.h>
main(void)
{
union employee {
int start_year;
int dpt_code;
int id_number;
} info;
/* initialize start_year */
info.start_year = 1997;
/* initialize dpt_code */
info.dpt_code = 8;
/* initialize id */
info.id_number = 1234;
/* display content of union */
printf("Start Year: %dn", info.start_year);
printf("Dpt. Code: %dn", info.dpt_code);
printf("ID Number: %dn", info.id_number);
return 0;
}
/* 20L03.c The size of a union */
#include <stdio.h>
#include <string.h>
main(void)
union u {
double x;
int y;
} a_union;
struct s {
double x;
int y;
} a_struct;
printf("The size of double: %d-byten",
sizeof(double));
printf("The size of int: %d-byten",
sizeof(int));
printf("The size of a_union: %d-byten",
sizeof(a_union));
printf("The size of a_struct: %d-byten",
sizeof(a_struct));
return 0;
}
Defining Bit Fields with struct
• a structure have their own memory locations.
struct tag_name {
data_type name1: length1;
data_type name2: lenght2;
. . .
data_type nameN: lengthN;
} variable_list;
struct horse
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
};
struct horse Dobbin = {
24, 17,"Dobbin", "Trigger", "Flossie"
};
struct horse Trigger = {
30, 15, "Trigger", "Smith", "Wesson"
};
Accessing Structure Members
Dobbin.age = 12;
#include <stdio.h>
int main(void)
{
/* Structure declaration */
struct horse
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
};
struct horse My_first_horse; /* Structure variable declaration */
/* Initialize the structure variable from input data */
printf("Enter the name of the horse: " );
scanf("%s", My_first_horse.name ); /* Read the horse's name */
printf("How old is %s? ", My_first_horse.name );
scanf("%d", &My_first_horse.age ); /* Read the horse's age */
printf("How high is %s ( in hands )? ", My_first_horse.name );
scanf("%d", &My_first_horse.height ); /* Read the horse's height */
printf("Who is %s's father? ", My_first_horse.name );
scanf("%s", My_first_horse.father ); /* Get the father's name */
printf("Who is %s's mother? ", My_first_horse.name );
scanf("%s", My_first_horse.mother ); /* Get the mother's name */
/* Now tell them what we know */
printf("n%s is %d years old, %d hands high,",
My_first_horse.name, My_first_horse.age, My_first_horse.height);
printf(" and has %s and %s as parents.n", My_first_horse.father,
My_first_horse.mother );
return 0;
}
/* 20L06.c: Applying bit fields */
#include <stdio.h>
#include <string.h>
struct bit_field {
int cable: 1;
int dish: 1;
};
struct survey {
char name[20];
struct bit_field c_d;
int age;
int hour_per_week;
union {
char cable_company[16];
char dish_company[16];
} provider;
};
void DataEnter(struct survey *s);
void DataDisplay(struct survey *s);
main(void)
{
struct survey tv;
DataEnter(&tv);
DataDisplay(&tv);
return 0;
}
/* function definition */
void DataEnter(struct survey *ptr)
{
char is_yes[4];
printf("Are you using cable at home? (Yes or No)n");
gets(is_yes);
if ((is_yes[0] == `Y') ||
(is_yes[0] == `y')){
printf("Enter the cable company name:n");
gets(ptr->provider.cable_company);
ptr->c_d.cable = 1;
ptr->c_d.dish = 0;
} else {
printf("Are you using a satellite dish? (Yes or No)n");
gets(is_yes);
if ((is_yes[0] == `Y') ||
(is_yes[0] == `y')){
printf("Enter the satellite dish company name:n");
gets(ptr->provider.dish_company);
ptr->c_d.cable = 0;
ptr->c_d.dish = 1;
} else {
ptr->c_d.cable = 0;
ptr->c_d.dish = 0;
}
}
printf("Please enter your name:n");
gets(ptr->name);
printf("Your age:n");
scanf("%d", &ptr->age);
printf("How many hours you spend on watching TV per week:n");
scanf("%d", &ptr->hour_per_week);
}
/* function definition */
void DataDisplay(struct survey *ptr)
{
printf("nHere's what you've entered:n");
printf("Name: %sn", ptr->name);
printf("Age: %dn", ptr->age);
printf("Hour per week: %dn", ptr->hour_per_week);
if (ptr->c_d.cable && !ptr->c_d.dish)
printf("Your cable company is: %sn",
ptr->provider.cable_company);
else if (!ptr->c_d.cable && ptr->c_d.dish)
printf("Your satellite dish company is: %sn",
ptr->provider.dish_company);
else
printf("You don't have cable or a satellite dish.n");
printf("nThanks and Bye!n");
}

More Related Content

Viewers also liked

A quick guide to virtualization
A quick guide to virtualizationA quick guide to virtualization
A quick guide to virtualizationJhonny Edward
 
Oyunu kim ve nasil değiştirir
Oyunu kim ve nasil değiştirirOyunu kim ve nasil değiştirir
Oyunu kim ve nasil değiştirirTanyer Sonmezer
 
Digital literacy and tips on attending managing an online course
Digital literacy and tips on attending managing an online courseDigital literacy and tips on attending managing an online course
Digital literacy and tips on attending managing an online courseIrina K
 
Dasar laut yang gelap bedasarkan alquran dan sains
Dasar laut yang gelap bedasarkan alquran dan sainsDasar laut yang gelap bedasarkan alquran dan sains
Dasar laut yang gelap bedasarkan alquran dan sainsPutera Amin
 
Human resource management notes mba
Human resource management notes mbaHuman resource management notes mba
Human resource management notes mbaBabasab Patil
 
2016 Resume Amy
2016 Resume Amy2016 Resume Amy
2016 Resume AmyAmy Neiman
 
Compiler, linker & loader
Compiler, linker & loaderCompiler, linker & loader
Compiler, linker & loaderRajani Singh
 
Presentation Profile Company
Presentation Profile CompanyPresentation Profile Company
Presentation Profile CompanyAhmad z'Rock
 
To Kill A Mockingbird (Children's edition)
To Kill A Mockingbird (Children's edition)To Kill A Mockingbird (Children's edition)
To Kill A Mockingbird (Children's edition)transfordete
 
Perform Group - NOAH13 London
Perform Group - NOAH13 LondonPerform Group - NOAH13 London
Perform Group - NOAH13 LondonNOAH Advisors
 
8 نکته طلایی از پیتردراکر که می تواند دنیای شما را تغییر دهد!
8 نکته طلایی از پیتردراکر که می تواند دنیای شما را تغییر دهد!8 نکته طلایی از پیتردراکر که می تواند دنیای شما را تغییر دهد!
8 نکته طلایی از پیتردراکر که می تواند دنیای شما را تغییر دهد!Modirinfo
 
INTERAKSI MANUSIA DAN LINGKUNGAN DALAM DINAMIKA ATMOSFER
INTERAKSI MANUSIA DAN LINGKUNGAN DALAM DINAMIKA ATMOSFERINTERAKSI MANUSIA DAN LINGKUNGAN DALAM DINAMIKA ATMOSFER
INTERAKSI MANUSIA DAN LINGKUNGAN DALAM DINAMIKA ATMOSFERNesha Mutiara
 
Bentuk muka bumi dan aktivitas penduduk
Bentuk muka bumi dan aktivitas pendudukBentuk muka bumi dan aktivitas penduduk
Bentuk muka bumi dan aktivitas pendudukDwi Cahyo
 
الإعلانات على الانترنت
الإعلانات على الانترنتالإعلانات على الانترنت
الإعلانات على الانترنتHatem Kameli
 
To Kill A Mockingbird Theme, Motifs, Symbols
To Kill A Mockingbird Theme, Motifs, SymbolsTo Kill A Mockingbird Theme, Motifs, Symbols
To Kill A Mockingbird Theme, Motifs, Symbolstranceking
 
Voyage en images
Voyage en imagesVoyage en images
Voyage en imagesLD Panne
 

Viewers also liked (18)

A quick guide to virtualization
A quick guide to virtualizationA quick guide to virtualization
A quick guide to virtualization
 
Oyunu kim ve nasil değiştirir
Oyunu kim ve nasil değiştirirOyunu kim ve nasil değiştirir
Oyunu kim ve nasil değiştirir
 
Digital literacy and tips on attending managing an online course
Digital literacy and tips on attending managing an online courseDigital literacy and tips on attending managing an online course
Digital literacy and tips on attending managing an online course
 
Dasar laut yang gelap bedasarkan alquran dan sains
Dasar laut yang gelap bedasarkan alquran dan sainsDasar laut yang gelap bedasarkan alquran dan sains
Dasar laut yang gelap bedasarkan alquran dan sains
 
Steven Bibby C V
Steven Bibby C VSteven Bibby C V
Steven Bibby C V
 
Human resource management notes mba
Human resource management notes mbaHuman resource management notes mba
Human resource management notes mba
 
2016 Resume Amy
2016 Resume Amy2016 Resume Amy
2016 Resume Amy
 
Fundamentals of management
Fundamentals of managementFundamentals of management
Fundamentals of management
 
Compiler, linker & loader
Compiler, linker & loaderCompiler, linker & loader
Compiler, linker & loader
 
Presentation Profile Company
Presentation Profile CompanyPresentation Profile Company
Presentation Profile Company
 
To Kill A Mockingbird (Children's edition)
To Kill A Mockingbird (Children's edition)To Kill A Mockingbird (Children's edition)
To Kill A Mockingbird (Children's edition)
 
Perform Group - NOAH13 London
Perform Group - NOAH13 LondonPerform Group - NOAH13 London
Perform Group - NOAH13 London
 
8 نکته طلایی از پیتردراکر که می تواند دنیای شما را تغییر دهد!
8 نکته طلایی از پیتردراکر که می تواند دنیای شما را تغییر دهد!8 نکته طلایی از پیتردراکر که می تواند دنیای شما را تغییر دهد!
8 نکته طلایی از پیتردراکر که می تواند دنیای شما را تغییر دهد!
 
INTERAKSI MANUSIA DAN LINGKUNGAN DALAM DINAMIKA ATMOSFER
INTERAKSI MANUSIA DAN LINGKUNGAN DALAM DINAMIKA ATMOSFERINTERAKSI MANUSIA DAN LINGKUNGAN DALAM DINAMIKA ATMOSFER
INTERAKSI MANUSIA DAN LINGKUNGAN DALAM DINAMIKA ATMOSFER
 
Bentuk muka bumi dan aktivitas penduduk
Bentuk muka bumi dan aktivitas pendudukBentuk muka bumi dan aktivitas penduduk
Bentuk muka bumi dan aktivitas penduduk
 
الإعلانات على الانترنت
الإعلانات على الانترنتالإعلانات على الانترنت
الإعلانات على الانترنت
 
To Kill A Mockingbird Theme, Motifs, Symbols
To Kill A Mockingbird Theme, Motifs, SymbolsTo Kill A Mockingbird Theme, Motifs, Symbols
To Kill A Mockingbird Theme, Motifs, Symbols
 
Voyage en images
Voyage en imagesVoyage en images
Voyage en images
 

Similar to 5. chapter iv

C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordZaibi Gondal
 
URGENTJavaPlease updated the already existing Java program and m.pdf
URGENTJavaPlease updated the already existing Java program and m.pdfURGENTJavaPlease updated the already existing Java program and m.pdf
URGENTJavaPlease updated the already existing Java program and m.pdferremmfab
 
How to sas codes and tricks
How to sas codes and tricksHow to sas codes and tricks
How to sas codes and tricksعاطف رضا
 
Building Your First Widget
Building Your First WidgetBuilding Your First Widget
Building Your First WidgetChris Wilcoxson
 
Date difference[1]
Date difference[1]Date difference[1]
Date difference[1]shafiullas
 
Get into the FLOW with Extbase
Get into the FLOW with ExtbaseGet into the FLOW with Extbase
Get into the FLOW with ExtbaseJochen Rau
 
Code to copy Person.java .pdf
Code to copy Person.java .pdfCode to copy Person.java .pdf
Code to copy Person.java .pdfanokhijew
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationBrent Shaffer
 
#include iostream #include fstream #include vector #incl.pdf
#include iostream #include fstream #include vector #incl.pdf#include iostream #include fstream #include vector #incl.pdf
#include iostream #include fstream #include vector #incl.pdfshahidqamar17
 
C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfajantha11
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)Mike Friedman
 

Similar to 5. chapter iv (13)

C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
 
URGENTJavaPlease updated the already existing Java program and m.pdf
URGENTJavaPlease updated the already existing Java program and m.pdfURGENTJavaPlease updated the already existing Java program and m.pdf
URGENTJavaPlease updated the already existing Java program and m.pdf
 
How to sas codes and tricks
How to sas codes and tricksHow to sas codes and tricks
How to sas codes and tricks
 
Building Your First Widget
Building Your First WidgetBuilding Your First Widget
Building Your First Widget
 
Date difference[1]
Date difference[1]Date difference[1]
Date difference[1]
 
Get into the FLOW with Extbase
Get into the FLOW with ExtbaseGet into the FLOW with Extbase
Get into the FLOW with Extbase
 
Code to copy Person.java .pdf
Code to copy Person.java .pdfCode to copy Person.java .pdf
Code to copy Person.java .pdf
 
structure
structurestructure
structure
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes Presentation
 
#include iostream #include fstream #include vector #incl.pdf
#include iostream #include fstream #include vector #incl.pdf#include iostream #include fstream #include vector #incl.pdf
#include iostream #include fstream #include vector #incl.pdf
 
C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdf
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)
 
styleSample
styleSamplestyleSample
styleSample
 

More from Chhom Karath

More from Chhom Karath (20)

set1.pdf
set1.pdfset1.pdf
set1.pdf
 
Set1.pptx
Set1.pptxSet1.pptx
Set1.pptx
 
orthodontic patient education.pdf
orthodontic patient education.pdforthodontic patient education.pdf
orthodontic patient education.pdf
 
New ton 3.pdf
New ton 3.pdfNew ton 3.pdf
New ton 3.pdf
 
ច្បាប់ញូតុនទី៣.pptx
ច្បាប់ញូតុនទី៣.pptxច្បាប់ញូតុនទី៣.pptx
ច្បាប់ញូតុនទី៣.pptx
 
Control tipping.pptx
Control tipping.pptxControl tipping.pptx
Control tipping.pptx
 
Bulbous loop.pptx
Bulbous loop.pptxBulbous loop.pptx
Bulbous loop.pptx
 
brush teeth.pptx
brush teeth.pptxbrush teeth.pptx
brush teeth.pptx
 
bracket size.pptx
bracket size.pptxbracket size.pptx
bracket size.pptx
 
arch form KORI copy.pptx
arch form KORI copy.pptxarch form KORI copy.pptx
arch form KORI copy.pptx
 
Bracket size
Bracket sizeBracket size
Bracket size
 
Couple
CoupleCouple
Couple
 
ច្បាប់ញូតុនទី៣
ច្បាប់ញូតុនទី៣ច្បាប់ញូតុនទី៣
ច្បាប់ញូតុនទី៣
 
Game1
Game1Game1
Game1
 
Shoe horn loop
Shoe horn loopShoe horn loop
Shoe horn loop
 
Opus loop
Opus loopOpus loop
Opus loop
 
V bend
V bendV bend
V bend
 
Closing loop
Closing loopClosing loop
Closing loop
 
Maxillary arch form
Maxillary arch formMaxillary arch form
Maxillary arch form
 
Front face analysis
Front face analysisFront face analysis
Front face analysis
 

Recently uploaded

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Recently uploaded (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 

5. chapter iv

  • 1. Another Way to Collect Dissimilar Data
  • 2. What Is a Union? • a block of memory that is used to hold data items of different types. – Declaring Unions & Defining Union Variables union automobile { int year; char model[8]; int engine_power; float weight;} sedan, pick_up, sport_utility; – Referring a Union with . or -> sedan.year = 1997; union automobile *ptr; ptr->year = 1997;
  • 3. Ex1: /* 20L01.c Referencing a union */ #include <stdio.h> #include <string.h> main(void) { union menu { char name[23]; double price; } dish; printf("The content assigned to the union separately:n"); /* reference name */ strcpy(dish.name, "Sweet and Sour Chicken"); printf("Dish Name: %sn", dish.name); /* reference price */ dish.price = 9.95; printf("Dish Price: %5.2fn", dish.price); return 0; }
  • 4. Ex2: #include <stdio.h> main(void) { union employee { int start_year; int dpt_code; int id_number; } info; /* initialize start_year */ info.start_year = 1997; /* initialize dpt_code */ info.dpt_code = 8; /* initialize id */ info.id_number = 1234; /* display content of union */ printf("Start Year: %dn", info.start_year); printf("Dpt. Code: %dn", info.dpt_code); printf("ID Number: %dn", info.id_number); return 0; }
  • 5. /* 20L03.c The size of a union */ #include <stdio.h> #include <string.h> main(void) union u { double x; int y; } a_union; struct s { double x; int y; } a_struct; printf("The size of double: %d-byten", sizeof(double)); printf("The size of int: %d-byten", sizeof(int)); printf("The size of a_union: %d-byten", sizeof(a_union)); printf("The size of a_struct: %d-byten", sizeof(a_struct)); return 0; }
  • 6. Defining Bit Fields with struct • a structure have their own memory locations. struct tag_name { data_type name1: length1; data_type name2: lenght2; . . . data_type nameN: lengthN; } variable_list;
  • 7. struct horse { int age; int height; char name[20]; char father[20]; char mother[20]; }; struct horse Dobbin = { 24, 17,"Dobbin", "Trigger", "Flossie" }; struct horse Trigger = { 30, 15, "Trigger", "Smith", "Wesson" }; Accessing Structure Members Dobbin.age = 12;
  • 8. #include <stdio.h> int main(void) { /* Structure declaration */ struct horse { int age; int height; char name[20]; char father[20]; char mother[20]; }; struct horse My_first_horse; /* Structure variable declaration */ /* Initialize the structure variable from input data */ printf("Enter the name of the horse: " ); scanf("%s", My_first_horse.name ); /* Read the horse's name */ printf("How old is %s? ", My_first_horse.name ); scanf("%d", &My_first_horse.age ); /* Read the horse's age */ printf("How high is %s ( in hands )? ", My_first_horse.name ); scanf("%d", &My_first_horse.height ); /* Read the horse's height */ printf("Who is %s's father? ", My_first_horse.name ); scanf("%s", My_first_horse.father ); /* Get the father's name */ printf("Who is %s's mother? ", My_first_horse.name ); scanf("%s", My_first_horse.mother ); /* Get the mother's name */ /* Now tell them what we know */ printf("n%s is %d years old, %d hands high,", My_first_horse.name, My_first_horse.age, My_first_horse.height); printf(" and has %s and %s as parents.n", My_first_horse.father, My_first_horse.mother ); return 0; }
  • 9. /* 20L06.c: Applying bit fields */ #include <stdio.h> #include <string.h> struct bit_field { int cable: 1; int dish: 1; }; struct survey { char name[20]; struct bit_field c_d; int age; int hour_per_week; union { char cable_company[16]; char dish_company[16]; } provider; }; void DataEnter(struct survey *s); void DataDisplay(struct survey *s); main(void) { struct survey tv; DataEnter(&tv); DataDisplay(&tv); return 0; } /* function definition */ void DataEnter(struct survey *ptr) { char is_yes[4]; printf("Are you using cable at home? (Yes or No)n"); gets(is_yes); if ((is_yes[0] == `Y') || (is_yes[0] == `y')){ printf("Enter the cable company name:n"); gets(ptr->provider.cable_company); ptr->c_d.cable = 1; ptr->c_d.dish = 0; } else { printf("Are you using a satellite dish? (Yes or No)n"); gets(is_yes); if ((is_yes[0] == `Y') || (is_yes[0] == `y')){ printf("Enter the satellite dish company name:n"); gets(ptr->provider.dish_company); ptr->c_d.cable = 0; ptr->c_d.dish = 1; } else { ptr->c_d.cable = 0; ptr->c_d.dish = 0; } } printf("Please enter your name:n"); gets(ptr->name);
  • 10. printf("Your age:n"); scanf("%d", &ptr->age); printf("How many hours you spend on watching TV per week:n"); scanf("%d", &ptr->hour_per_week); } /* function definition */ void DataDisplay(struct survey *ptr) { printf("nHere's what you've entered:n"); printf("Name: %sn", ptr->name); printf("Age: %dn", ptr->age); printf("Hour per week: %dn", ptr->hour_per_week); if (ptr->c_d.cable && !ptr->c_d.dish) printf("Your cable company is: %sn", ptr->provider.cable_company); else if (!ptr->c_d.cable && ptr->c_d.dish) printf("Your satellite dish company is: %sn", ptr->provider.dish_company); else printf("You don't have cable or a satellite dish.n"); printf("nThanks and Bye!n"); }