Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.

Similar presentations


Presentation on theme: "Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University."— Presentation transcript:

1 Chapter 6 Structures Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2 2Ku-Yaw ChangStructures Structures A structure A collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. A collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Also called records in some language, notably Pascal Also called records in some language, notably Pascal Examples Examples An employee is described by a set of attributes Name Name Address Address Social security number Social security number Salary Salary A point is a pair of coordinates A rectangle is a pair of points A rectangle is a pair of points

3 3Ku-Yaw ChangStructures 6.1 Basics of Structures A point x coordinate (integer) x coordinate (integer) y coordinate (integer) y coordinate (integer) The above two components can be placed in a structure declared like this: struct point { int x; int y; }; A structure tag A structure tag an optional name: point Members Members x and y

4 4Ku-Yaw ChangStructures 6.1 Basics of Structures More examples struct card { char * face; char * suit; }; struct card { char * face; char * suit; }; struct employee { char firstName[20]; char lastName[20]; int age; char gender; double hourlySalary; struct employee person; /* error*/ struct employee * ePter; /* pointer */ };

5 5Ku-Yaw ChangStructures 6.1 Basics of Structures A structure declaration Reserve no storage (memory) Reserve no storage (memory) Structure variables are defined like variables of other types struct point pt; struct point pt; struct card aCard, deck[52], * cardPtr; struct card aCard, deck[52], * cardPtr; struct card { char * face; char * suit; } aCard, deck[52], * cardPtr; struct card { char * face; char * suit; } aCard, deck[52], * cardPtr;

6 6Ku-Yaw ChangStructures 6.1 Basics of Structures A member of a particular structure is referred to in an expression by a construction of the form – dot operator structure-name. member For example, To print the coordinates of the point pt To print the coordinates of the point pt printf(“%d, %d”, pt.x, pt.y); To compute the distance from the origin (0,0) to pt To compute the distance from the origin (0,0) to pt double dist, sqrt(double); dist = sqrt( (double)pt.x * pt.x + (double)pt.y * pt.y)

7 7Ku-Yaw ChangStructures 6.1 Basics of Structures A member of a particular structure can also be referred by arrow operator For example, struct card aCard, * cardPtr; cardPtr = &aCard; printf(“%s”, cardPrt->suit); struct card aCard, * cardPtr; cardPtr = &aCard; printf(“%s”, cardPrt->suit);

8 8Ku-Yaw ChangStructures 6.1 Basics of Structures Structures can be nested struct rect { struct point pt1; struct point pt2; }; struct rect { struct point pt1; struct point pt2; }; For example, struct rect screen; screen.pt1.x struct rect screen; screen.pt1.x

9 9Ku-Yaw ChangStructures 6.7 typedef typedef Create new data type names Create new data type names typedef int Length; Make the name Length a synonym for int Make the name Length a synonym for int Length len, maxlen; Length len, maxlen; typedef char * String; Make String a synonym for char * or character pointer Make String a synonym for char * or character pointer String p; String p; Typedef struct tnode * Treeptr;

10 10Ku-Yaw ChangStructures 6.8 Unions A union A variable that may hold (at different times) objects of different types and sizes A variable that may hold (at different times) objects of different types and sizes Provide a way to manipulate different kinds of data in a single area of storage Provide a way to manipulate different kinds of data in a single area of storage Example Example union u_tag { int ival; floatfval; char *sval; } u; The variable u will be large enough to hold the largest of three types.

11 To be continued…


Download ppt "Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University."

Similar presentations


Ads by Google