Structures, Unions, and Enumerations Chapter 16
2 Data Structure ( 資料結構 ) Content of data –Attributes of an object Person:name, age, sex, … A poker card:suit ( 花色 ), face ( 點數 ) –Columns of a record Book-Borrowing:book_title, book_ID, borrower, date It is convenient if we can save them in a structure. –data structure ( 資料結構 )
3 Structure Types (16.2) Syntax: struct structureName{ dataType member1; dataType member2;... }; Ex: struct personData{ char name[20]; int age; bool sex; }; A pair of curve parentheses NOTE!! Ended with ;
4 Structure Variables (16.1) struct defines a new variable type ( 資料 型態 ), not a new variable. We can use a defined structure type to define variables. Ex: personData player1, player2; personData student[10];
5 Declaring Structure Variables struct personData{ char name[20]; int age; bool sex; }; personData player1, player2; player1player2
6 Declaring Structure Variables struct personData{ char name[20]; int age; bool sex; }; personData player1 = {" 莊孝維 ", 19, false}, player2 = {" 林志零 ", 20, true}; player1player2 agesex name agesex name members; fields records
7 Declaring Structure Array (16.3) personData student[10]; student[0]student[9] ‧‧‧ student[] student[1]
8 Initializing When Defining (16.3) Ex: personData student[10]={ {" 莊孝維 ", 19, false}, // student[0] {" 林志零 ", 20, true}, // student[1]... //name,age,sex }; The initial data are given in the order of members in the structure and the order of elements in the array.
9 Declaring Structure Variables The following statements are also legal: struct personData a1,student[10]; struct info{ int a,b; } info1, info2; struct { int a,b; } info1, info2; define variables immediately after the definition of a structure
10 Accessing Members of Structure '.' : structure member operator (dot operator) player1 player1.name player1.sex player1.age See ->
11 Accessing Members of Structure '.' : structure member operator (dot operator) –Ex: personData player1; strcpy( player1.name, " 莊孝維 "); player1.age = 19; player1.sex = false; // true for female, false for male player1 agesex name
12 Practice Define a data structure to store data of a student. –What attributes do we need to save? What are their types? Define 100 users. Input some data (not necessary 100) from the keyboard. Print these data out.
13 Pointer to A Structure Ex: personData player1, *mPtr; mPtr = &player1; player1 mPtr
14 Accessing Members of Structure '->' : structure pointer operator (arrow operator) player1 mPtr->name mPtr->sex mPtr->age mPtr See.
15 Accessing Members of Structure '->' : structure pointer operator (arrow operator) – 例: personData player1, *mPtr=&player1; strcpy(mPtr->name, " 莊孝維 "); mPtr->age = 19; mPtr->sex = false; mPtr player1 agesex name
16 Dot operator has higher preference than dereferencing operator. Accessing Members of Structure personData player1, *mPtr; mPtr = &player1; scanf("%d", &player1.age); scanf("%d", &(player1.age)); printf("%d\n", player1.age); printf("%d\n", mPtr.age); printf("%d\n", mPtr->age); printf("%d\n", (*mPtr).age); printf("%d\n", *mPtr.age);
17 Pointer to A Structure NOTE!! A pointer is just a variable with 4 bytes. No memory of its reference type will be allocated at definition time. –Hence you have to define a real variable of this type and let pointer refer to it before you use this pointer. Ex: personData *mPtr; personData player1; mPtr = &player1; player1 mPtr
18 Structure Assignment You can assign a variable's content as the other variable's content directly by = if they are of the same structure. struct data{ int age,sex; }; data player1, player2; player1.age = 34; player1.sex = 1; player2 = player1; player1player2 age sex 341 1
19 Structure Comparison The same as strings, you cannot compare two variables of the same structure by ==. For strings (i.e. char *), == means two pointers refer to the same address. For structures, == is undefined. if (player1 == player2) … Compile error C2676: binary '==' : 'struct person' does not define this operator or a conversion to a type acceptable to the predefined operator
20 Structure Comparison So… –Write a function to compare two variables of the same structure by comparing all the members of the structure.
21 Passing Structure as Parameters myfunc(personData manager); Note that it is call by value! If you want to modify the content of the variable, please call by reference. myfunc(personData *manager); __ ↑ use structure name as variable type __ ↑ call by reference
22 Practice Write a function to compare if two personData variables have the same values. Write a function to initialize a personData variable as follows: {" 無名氏 ", 0, true} struct personData{ char name[20]; int age; bool sex; };
struct city { char name[20]; int population; double area; }; name areapopulation Taipei name areapopulation Keelung city Taipei = {"Taipei", }, Keelung = {"Keelung", }; Objects vs. Structures An Example
agename birthplacelivingplace struct city { char name[20]; int population; double area; }; struct person { char name[20]; int age; city *birthplace, *livingplace; }; name areapopulation Taipei name areapopulation Keelung person Mary = {"Mary", 32}, Susan = {"Susan", 5}; ? ? Mary agename birthplacelivingplace ?? Susan
agename birthplacelivingplace struct city { char name[20]; int population; double area; }; struct person { char name[20]; int age; city *birthplace, *livingplace; }; name areapopulation Taipei name areapopulation Keelung // Mary was born in Keelung and lives in Taipei now Mary.birthplace = &Keelung; Mary.livingplace = &Taipei; ? ? Mary agename birthplacelivingplace ?? Susan
agename birthplacelivingplace struct city { char name[20]; int population; double area; }; struct person { char name[20]; int age; city *birthplace, *livingplace; }; name areapopulation Taipei name areapopulation Keelung // Susan lives with Mary Susan.livingplace = Mary.livingplace; Mary agename birthplacelivingplace ?? Susan
27 Nested Structures (16.3) A member of a structure can be a structure of another type. Ex: struct dateData{ int year, month, day; }; struct personData{ char name[20]; dateData birthdate; }; personData mySon; mySon.birthdate.day = 26; mySon name birthdate yearmonthday 26
28 typedef (16.2) To give an alias name –typedef int ageType; ageType age[20]; To give a shorter name for "struct yourStructureName" –typedef struct personData pType; pType student[20]; original name new name original name new name
29 Examples of typedef typedef int ageType; typedef struct personData pType; typedef personData * personPtr; typedef struct{ char id[13],nickname[100]; int loginTimes,status; } bbsIDdata;