5. chapter iv

10
Another Way to Collect Dissimilar Data

Transcript of 5. chapter iv

Page 1: 5. chapter iv

Another Way to Collect Dissimilar Data

Page 2: 5. chapter iv

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;

Page 3: 5. chapter iv

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: %s\n", dish.name); /* reference price */ dish.price = 9.95; printf("Dish Price: %5.2f\n", dish.price); return 0;}

Page 4: 5. chapter iv

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: %d\n", info.start_year); printf("Dpt. Code: %d\n", info.dpt_code); printf("ID Number: %d\n", info.id_number); return 0;}

Page 5: 5. chapter iv

/* 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-byte\n", sizeof(double)); printf("The size of int: %d-byte\n", sizeof(int)); printf("The size of a_union: %d-byte\n", sizeof(a_union)); printf("The size of a_struct: %d-byte\n", sizeof(a_struct)); return 0;}

Page 6: 5. chapter iv

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;

Page 7: 5. chapter iv

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;

Page 8: 5. chapter iv

#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;}

Page 9: 5. chapter iv

/* 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);

Page 10: 5. chapter iv

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: %s\n", ptr->name); printf("Age: %d\n", ptr->age); printf("Hour per week: %d\n", ptr->hour_per_week); if (ptr->c_d.cable && !ptr->c_d.dish) printf("Your cable company is: %s\n", ptr->provider.cable_company); else if (!ptr->c_d.cable && ptr->c_d.dish) printf("Your satellite dish company is: %s\n", ptr->provider.dish_company); else printf("You don't have cable or a satellite dish.\n"); printf("\nThanks and Bye!\n"); }