29
Structures, Unions, and Enumerations Chapter 16

Structures, Unions, and Enumerations Chapter 16. 2 Data Structure ( 資料結構 ) Content of data –Attributes of an object Person:name, age, sex, … A poker card:suit

Embed Size (px)

Citation preview

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, borrowe

r, 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;

age: int sex: bool

name: char[20]player1

age: int sex: bool

name: char[20]player2

6

Declaring Structure Variables struct personData{

char name[20]; int age; bool sex;};

personData player1 = {" 莊孝維 ", 19, false},player2 = {" 林志零 ", 20, true};

19 false

莊孝維player1

20 true

林志零player2

age sex

name

age sex

name

members; fields

records

7

Declaring Structure Array (16.3) personData student[10];

age: int sex: bool

name: char[20]student[0] student[9]

‧‧‧

student[]

age: int sex: bool

name: char[20]

age: int sex: bool

name: char[20]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 inthe order of members in the structure andthe 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)

age: int sex: bool

name: char[20]player1 player1.name

player1.sexplayer1.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

19 false

莊孝維player1

age sex

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;

age: int sex: bool

name: char[20]player1

mPtr

14

Accessing Members of Structure'->': structure pointer operator

(arrow operator)

age: int sex: bool

name: char[20]player1 mPtr->name

mPtr->sexmPtr->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

19 false

莊孝維player1

age sex

name

16Dot operator has higher preference thandereferencing 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;

age: int sex: bool

name: char[20]player1mPtr

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;

player1 player2age agesex sex

34 1 34 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 pointer

s 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

areapopulation2000000 0

"Taipei"Taipei

name

areapopulation200000 0

"Keelung"Keelung

city Taipei = {"Taipei", 2000000}, Keelung = {"Keelung", 200000};

Objects vs. StructuresAn Example

agename

birthplace livingplace

struct city { char name[20]; int population; double area;};

struct person { char name[20]; int age; city *birthplace, *livingplace;};

name

areapopulation2000000 0

"Taipei"Taipei

name

areapopulation200000 0

"Keelung"Keelung

person Mary = {"Mary", 32}, Susan = {"Susan", 5};

"Mary" 32

? ?

Mary

agename

birthplace livingplace"Susan" 5

? ?

Susan

agename

birthplace livingplace

struct city { char name[20]; int population; double area;};

struct person { char name[20]; int age; city *birthplace, *livingplace;};

name

areapopulation2000000 0

"Taipei"Taipei

name

areapopulation200000 0

"Keelung"Keelung

// Mary was born in Keelung and lives in Taipei nowMary.birthplace = &Keelung;Mary.livingplace = &Taipei;

"Mary" 32

? ?

Mary

agename

birthplace livingplace"Susan" 5

? ?

Susan

agename

birthplace livingplace

struct city { char name[20]; int population; double area;};

struct person { char name[20]; int age; city *birthplace, *livingplace;};

name

areapopulation2000000 0

"Taipei"Taipei

name

areapopulation200000 0

"Keelung"Keelung

// Susan lives with MarySusan.livingplace = Mary.livingplace;

"Mary" 32Mary

agename

birthplace livingplace"Susan" 5

? ?

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;

mySonname

birthdate

year month day26

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 typedeftypedef int ageType;typedef struct personData pType;typedef personData * personPtr;typedef struct{ char id[13],nickname[100]; int loginTimes,status;} bbsIDdata;