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");
}

5. chapter iv

  • 1.
    Another Way toCollect Dissimilar Data
  • 2.
    What Is aUnion? • 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 Referencinga 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 Thesize 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 Fieldswith 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; intheight; 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: Applyingbit 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("Howmany 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"); }