110 likes | 227 Vues
This chapter introduces data structures, defined as collections of variables grouped under a single name for simplified handling. It covers the basics of structures, including declarations, members, and nesting. Examples include structures for points, cards, and employees, highlighting how to manipulate these using dot and arrow operators. Additionally, typedefs for creating synonyms for data types and an introduction to unions are discussed. Through practical examples, readers learn to effectively use structures in programming.
E N D
Chapter 6Structures Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
Structures • A structure • 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 • Examples • An employee is described by a set of attributes • Name • Address • Social security number • Salary • A point is a pair of coordinates • A rectangle is a pair of points Structures
6.1 Basics of Structures • A point • x 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 • an optional name: point • Members • x and y Structures
More examples 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 */}; 6.1 Basics of Structures Structures
6.1 Basics of Structures • A structure declaration • Reserve no storage (memory) • Structure variables are defined like variables of other types • struct point pt; • struct card aCard, deck[52], * cardPtr; • struct card { char * face; char * suit;} aCard, deck[52], * cardPtr; Structures
6.1 Basics of Structures • A member of a particular structure is referred to in an expression by a construction of the form – dot operatorstructure-name . member • For example, • 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 • double dist, sqrt(double);dist = sqrt( (double)pt.x * pt.x + (double)pt.y * pt.y) Structures
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); Structures
6.1 Basics of Structures • Structures can be nested • struct rect { struct point pt1; struct point pt2;}; • For example, • struct rect screen;screen.pt1.x Structures
6.7 typedef • typedef • Create new data type names • typedef int Length; • Make the name Length a synonym for int • Length len, maxlen; • typedef char * String; • Make String a synonym for char * or character pointer • String p; • Typedef struct tnode * Treeptr; Structures
6.8 Unions • A union • 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 • Example • union u_tag {intival; float fval; char * sval;} u; • The variable u will be large enough to hold the largest of three types. Structures