structure and union types
DESCRIPTION
Structure and Union Types. 程式設計 潘仁義 CCU COMM. Structure Type Definition. struct structured data objects, can be defined by users - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/1.jpg)
Structure and Union TypesStructure and Union Types
程式設計潘仁義
CCU COMM
![Page 2: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/2.jpg)
Structure Type DefinitionStructure Type Definitionstruct
structured data objects, can be defined by users
#define STRSIZ 10
typedef 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(¤t_planet); /* 等會看 */
current_planet = get_planet();print_planet(current_planet);…
![Page 3: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/3.jpg)
Assigning Values to Components Assigning Values to Components of Variable current_planetof Variable current_planet
![Page 4: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/4.jpg)
Function with a Structured Input ParameterFunction with a Structured Input Parameter
![Page 5: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/5.jpg)
Function Comparing Two Structured Values Function Comparing Two Structured Values for Equalityfor Equality
![Page 6: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/6.jpg)
Function with a Structured Output ArgumentFunction with a Structured Output Argument
也可寫成& plnp->diameter,&plnp->moons,& plnp->orbit_time,&plnp->rotation_time);
![Page 7: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/7.jpg)
Data Areas of main and scan_planet during Data Areas of main and scan_planet during Execution of status = scan_planet (¤tExecution of status = scan_planet (¤t_planet);_planet);
![Page 8: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/8.jpg)
Function get_planet Returning a Structured Function get_planet Returning a Structured Result TypeResult Type
![Page 9: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/9.jpg)
Data Type planet_t and Basic OperationsData Type planet_t and Basic Operations
![Page 10: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/10.jpg)
Parallel Arrays and an Array of StructuresParallel Arrays and an Array of Structures
int id[50];double gpa[50];
struct {int id;double gpa;
} stulist[50];
![Page 11: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/11.jpg)
Union typesUnion types
UnionTo 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;
![Page 12: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/12.jpg)
Function That Displays a Structure with a Function That Displays a Structure with a Union Type ComponentUnion Type Component
![Page 13: Structure and Union Types](https://reader035.vdocuments.net/reader035/viewer/2022062409/56814636550346895db34519/html5/thumbnails/13.jpg)
Q & AQ & A
union 可以用 -> 嗎 ?
struct planet_t a, b;a = b; /* 可這麼寫嗎 ? */if(a == b) {} /* 可這麼寫嗎 ? */
(*plnp).name*plnp.nameplnp->name&plnp->name
小心: union 的內容該如何解釋 ?