Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10.

Similar presentations


Presentation on theme: "© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10."— Presentation transcript:

1 © 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10

2 1-2 © 2012 Pearson Addison-Wesley. All rights reserved. Outline User-defined structure types Structure type data as input and output parameter Functions whose result values are structured Union Type 2

3 1-3 © 2012 Pearson Addison-Wesley. All rights reserved. User-defined structure types Syntax: Example 1 typedef statement allocates no memory. A later declaration creates a variable built on the template of the type definition, will create contagious area in memory for all members of the struct. 3 typedef struct{ type1 name1; type2 name2;....... }struct type; typedef struct{ /*complex number structure*/ double real; double imaginary; }complex_t;

4 1-4 © 2012 Pearson Addison-Wesley. All rights reserved. Example 2 4 typedef struct{ char name[10]; double diameter; int moons; double orbit_time, rotation_time; }planet_t; int main (void) { int x; planet_t current_planet; planet_t previous_planet; planet_t blank_planet = { " ", 0, 0, 0, 0}; float y; }

5 1-5 © 2012 Pearson Addison-Wesley. All rights reserved. Manipulating a struct 1.Manipulating individual component –Ex: 2.Manipulating whole structure –previous_planet = current_planet; 5 strcpy(current_planet.name, "Jupiter"); current_planet.diameter = 142980; current_planet.moons = 16; current_planet.orbit_time = 11.9; current_planet.rotation_time = 9.925;

6 1-6 © 2012 Pearson Addison-Wesley. All rights reserved. As an input and output parameters The following function prints the contents of the struct 6 void print_planet(planet_t pa) { printf("%s\n", pa.name); printf("the diameter is: %f km\n", pa.diameter); printf("number of moons : %d \n", pa.moons); printf("orbit time is : %f years\n", pa.orbit_time); } main() { int x; print_planet(current_planet); }

7 1-7 © 2012 Pearson Addison-Wesley. All rights reserved. Passing a Struct as an Output Argument 7 /* * Fills a type planet_t structure with input data. Integer returned as * function result is success/failure/EOF indicator. * 1 => successful input of one planet * 0 => error encountered * EOF => insufficient data before end of file * In case of error or EOF, value of type planet_t output argument is * undefined. */ int scan_planet(planet_t *plnp) { /* output - address of planet_t structure to fill */ int result; result = scanf("%s%lf%d%lf%lf", (*plnp).name, if (result == 5) result = 1; else if (result != EOF) result = 0; return (result); }

8 1-8 © 2012 Pearson Addison-Wesley. All rights reserved. Pass by reference memory content 8

9 1-9 © 2012 Pearson Addison-Wesley. All rights reserved. Functions whose result values are structured 9 planet_t get_planet(void) { planet_t planet; scanf("%s%lf%d%lf%lf", planet.name, &planet.diameter, &planet.moons, &planet.orbit_time, &planet.rotation_time); return(planet); } main() { planet_t current_planet; current_planet = get_planet(); }

10 1-10 © 2012 Pearson Addison-Wesley. All rights reserved. Figure 10.11 An Array of Structures 1-10

11 1-11 © 2012 Pearson Addison-Wesley. All rights reserved. Functions whose result values are structured 11 #define MAX_STU 50 #define NUM_PTS 10 typedef struct { int id; double gpa; } student_t; typedef struct { double x, y; } point_t; main() { student_t stulist[MAX_STU]; point_t polygon[NUM_PTS]; for (i = 0; i < MAX_STU; ++i) scan_student(&stulist[i]); for (i = 0; i < MAX_STU; ++i) printf("%d\n", stulist[i].id); } Implement “scan_student” function

12 1-12 © 2012 Pearson Addison-Wesley. All rights reserved. Union Unions are structured types in which some components vary depending on the value of another component. Example: to compute are and circumference of a geometric shape, we will need different data based on the shape processed: radius for a circle, side length for a square, width and height for a rectangle 12

13 1-13 © 2012 Pearson Addison-Wesley. All rights reserved. Example 1 This variable hair_data does not contain both wears_wig and color components. Rather, it has either a wears_wig component referenced by hair_data.wears_ wig or a color component referenced by hair_data.color. When memory is allocated for hair_data, the amount of memory is determined by the largest component of the union. 13 typedef union { int wears_wig; char color[20]; } hair_t; hair_t hair_data; typedef struct { int bald; hair_t h; } hair_info_t;

14 1-14 © 2012 Pearson Addison-Wesley. All rights reserved. Function That Displays a Structure with a Union Type Component 14 Void print_hair_info(hair_info_t hair) { /* input – structure to display */ if (hair.bald) { printf("Subject is bald"); if (hair.h.wears_wig) printf(", but wears a wig.\n"); else printf(" and does not wear a wig.\n"); } else { printf("Subject's hair color is %s.\n", hair.h.color); }

15 1-15 © 2012 Pearson Addison-Wesley. All rights reserved. Two Interpretations of Parameter hair 15

16 1-16 © 2012 Pearson Addison-Wesley. All rights reserved. Example 2 16 /* Types defining the components needed to represent each shape. */ typedef struct { double area, circumference, radius; } circle_t; typedef struct { double area, perimeter, width, height; } rectangle_t; typedef struct { double area, perimeter, side; } square_t; /* Type of a structure that can be interpreted a different way for each shape */ typedef union { circle_t circle; rectangle_t rectangle; square_t square; } figure_data_t; /* Type containing a structure with multiple interpretations along with a component whose value indicates the current valid interpretation */ typedef struct { char shape; figure_data_t fig; } figure_t;

17 1-17 © 2012 Pearson Addison-Wesley. All rights reserved. Exercise: Implement incomplete functions 17 figure_t get_figure_dimensions(void); figure_t compute_area(figure_t object); figure_t compute_perim(figure_t object); void print_figure(figure_t object); int main(void) { figure_t onefig; printf("Area and Perimeter Computation Program\n"); for (onefig = get_figure_dimensions(); onefig.shape != 'Q'; onefig = get_figure_dimensions()) { onefig = compute_area(onefig); onefig = compute_perim(onefig); print_figure(onefig); } return (0); }


Download ppt "© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10."

Similar presentations


Ads by Google