1 / 11

Understanding Data Structures: Definitions, Examples, and Usage in Programming

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.

kyria
Télécharger la présentation

Understanding Data Structures: Definitions, Examples, and Usage in Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. 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

  5. 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. 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

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

  8. 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

  9. 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

  10. 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

  11. To be continued…

More Related