chapter 6 structures

11
Chapter 6 Chapter 6 Structures Structures Ku-Yaw Chang Ku-Yaw Chang [email protected] Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information Engineering Computer Science and Information Engineering Da-Yeh University Da-Yeh University

Upload: kyria

Post on 26-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Chapter 6 Structures. Ku-Yaw Chang [email protected] Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University. Structures. A structure - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 6 Structures

Chapter 6Chapter 6StructuresStructures

Ku-Yaw ChangKu-Yaw [email protected]

Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information EngineeringComputer Science and Information Engineering

Da-Yeh UniversityDa-Yeh University

Page 2: Chapter 6 Structures

22Ku-Yaw ChangKu-Yaw Chang StructuresStructures

StructuresStructures

A A structurestructure A collection of one or more variables, possibly of different types, A collection of one or more variables, possibly of different types,

grouped together under a single name for convenient handling.grouped together under a single name for convenient handling. Also called Also called recordsrecords in some language, notably Pascal in some language, notably Pascal ExamplesExamples

An employee is described by a set of attributesAn employee is described by a set of attributes NameName AddressAddress Social security numberSocial security number SalarySalary

A point is a pair of coordinatesA point is a pair of coordinates A rectangle is a pair of pointsA rectangle is a pair of points

Page 3: Chapter 6 Structures

33Ku-Yaw ChangKu-Yaw Chang StructuresStructures

6.1 Basics of Structures6.1 Basics of Structures

A pointA point x coordinate (integer)x coordinate (integer) y coordinate (integer)y coordinate (integer)

The above two components can be placed in a structure The above two components can be placed in a structure declared like this:declared like this:

structstruct point { point { int x; int x; int y; int y;

}};; A structure A structure tagtag

an optional name: pointan optional name: point MembersMembers

x and yx and y

Page 4: Chapter 6 Structures

44Ku-Yaw ChangKu-Yaw Chang StructuresStructures

6.1 Basics of Structures6.1 Basics of Structures

More examplesMore examples struct card {struct card {

char * face; char * face; char * suit; char * suit;};};

struct employee {struct employee { char firstName[20]; char firstName[20]; char lastName[20]; char lastName[20]; int age; int age; char gender; char gender; double hourlySalary; double hourlySalary; struct employee person; /* error*/struct employee person; /* error*/ struct employee * ePter; /* pointer */ struct employee * ePter; /* pointer */};};

Page 5: Chapter 6 Structures

55Ku-Yaw ChangKu-Yaw Chang StructuresStructures

6.1 Basics of Structures6.1 Basics of Structures

A structure declarationA structure declaration Reserve no storage (memory)Reserve no storage (memory)

Structure variables are defined like variables of other Structure variables are defined like variables of other typestypes

struct point pt;struct point pt; struct card aCard, deck[52], * cardPtr;struct card aCard, deck[52], * cardPtr; struct card {struct card {

char * face; char * face; char * suit; char * suit;} aCard, deck[52], * cardPtr;} aCard, deck[52], * cardPtr;

Page 6: Chapter 6 Structures

66Ku-Yaw ChangKu-Yaw Chang StructuresStructures

6.1 Basics of Structures6.1 Basics of Structures

A member of a particular structure is referred to in an A member of a particular structure is referred to in an expression by a construction of the form – expression by a construction of the form – dot dot operatoroperator structure-name . memberstructure-name . member

For example, For example, To print the coordinates of the point ptTo print the coordinates of the point pt

printf(“%d, %d”, printf(“%d, %d”, pt.xpt.x, , pt.ypt.y);); To compute the distance from the origin (0,0) to ptTo compute the distance from the origin (0,0) to pt

double dist, sqrt(double);double dist, sqrt(double);dist = sqrt( (double)pt.x * pt.x + (double)pt.y * pt.y)dist = sqrt( (double)pt.x * pt.x + (double)pt.y * pt.y)

Page 7: Chapter 6 Structures

77Ku-Yaw ChangKu-Yaw Chang StructuresStructures

6.1 Basics of Structures6.1 Basics of Structures

A member of a particular structure can also be referred A member of a particular structure can also be referred by by arrow arrow operatoroperator

For example, For example, struct card aCard, * cardPtr;struct card aCard, * cardPtr;

cardPtr = &aCard;cardPtr = &aCard;printf(“%s”, printf(“%s”, cardPrt->suitcardPrt->suit););

Page 8: Chapter 6 Structures

88Ku-Yaw ChangKu-Yaw Chang StructuresStructures

6.1 Basics of Structures6.1 Basics of Structures

Structures can be nestedStructures can be nested struct rect {struct rect {

struct point pt1; struct point pt1; struct point pt2; struct point pt2;};};

For example,For example, struct rect screen;struct rect screen;

screen.pt1.x screen.pt1.x

Page 9: Chapter 6 Structures

99Ku-Yaw ChangKu-Yaw Chang StructuresStructures

6.7 typedef6.7 typedef

typedeftypedef Create new data type namesCreate new data type names

typedef int Length;typedef int Length; Make the name Length a synonym for intMake the name Length a synonym for int Length len, maxlen;Length len, maxlen;

typedef char * String;typedef char * String; Make String a synonym for char * or character pointerMake String a synonym for char * or character pointer String p;String p;

Typedef struct tnode * Treeptr;Typedef struct tnode * Treeptr;

Page 10: Chapter 6 Structures

1010Ku-Yaw ChangKu-Yaw Chang StructuresStructures

6.8 Unions6.8 Unions

A A unionunion A variable that may hold (at different times) objects of different A variable that may hold (at different times) objects of different

types and sizestypes and sizes Provide a way to manipulate different kinds of data in a single Provide a way to manipulate different kinds of data in a single

area of storagearea of storage ExampleExample

union u_tag {union u_tag {int int ival;ival;floatfloat fval;fval;char *char * sval;sval;

} u;} u;

The variable u will be large enough to hold the largest of three types.The variable u will be large enough to hold the largest of three types.

Page 11: Chapter 6 Structures

To be continued…To be continued…