1 / 13

Structure and Union Types

Structure and Union Types. 程式設計 潘仁義 CCU COMM. Structure Type Definition. struct structured data objects, can be defined by users

sylvia
Télécharger la présentation

Structure and Union Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structure and Union Types 程式設計 潘仁義 CCU COMM

  2. Structure Type Definition • struct • structured data objects, can be defined by users • #define STRSIZ 10typedef struct { char name[STRSIZ]; double diameter; /* diameter in km */ int moons; /* number of moons */ double orbit_time, /* years to orbit sun once */ rotation_time; /* hours to rotate once */} planet_t; • int main (){ planet_t current_planet, blank_planet = {“”, 0,0,0,0}; int status = scan_planet(&current_planet); /* 等會看 */ current_planet = get_planet(); print_planet(current_planet); …

  3. Assigning Values to Components of Variable current_planet

  4. Function with a Structured Input Parameter

  5. Function Comparing Two Structured Values for Equality

  6. Function with a Structured Output Argument 也可寫成 & plnp->diameter, &plnp->moons, & plnp->orbit_time, &plnp->rotation_time);

  7. Data Areas of main and scan_planet during Execution of status = scan_planet (&current_planet);

  8. Function get_planet Returning a Structured Result Type

  9. Data Type planet_t and Basic Operations

  10. Parallel Arrays and an Array of Structures • int id[50];double gpa[50]; • struct { int id; double gpa;} stulist[50];

  11. Union types • Union • To deal with situations in which one needs a data object that can be interpreted in a variety of ways. • typedef union { int wears_wig; /*載假髮嗎?*/ char color[20];} hair_t; • typedef struct { int bald; /*禿頭嗎?*/ hair_t h;} hair_info_t;

  12. Function That Displays a Structure with a Union Type Component

  13. Q & A • union 可以用 -> 嗎? • struct planet_t a, b;a = b; /* 可這麼寫嗎? */if(a == b) {} /* 可這麼寫嗎? */ • (*plnp).name*plnp.nameplnp->name&plnp->name • 小心:union 的內容該如何解釋?

More Related