Unformatted text preview:

University of Washington Computer Programming I Lecture 18 Structures 2000 UW CSE S 1 Concepts this lecture Review Data structures Heterogenous structures structs records struct type definitions typedef Field selection operator Structs as parameters Call by value Pointer parameters and operator S 2 Review Data Structures Functions give us a way to organize programs Data structures are needed to organize data especially 1 large amounts of data 2 variable amounts of data 3 sets of data where the individual pieces are related to one another Arrays helped with points 1 and 2 but not with point 3 Example the data describing one house in a neighborhood x y color windows etc Example information about one student name ID GPA etc etc S 4 Problem Account Records The Engulf Devour Credit Co Inc Ltd needs to keep track of insurance policies it has issued Information recorded for each policy Account number integer Policy holder s age integer and sex m or f Monthly premium double At E G customers are only known by their account so there is no need to store their names S 5 Structs Heterogeneous Structures Collection of values of possibly differing types Name the collection name the components fields Example Insurance policy information for Alice informally alice account 9501234 age 23 sex f premium 42 17 C expressions alice age is 23 alice sex is f 2 alice premium is 84 34 S 6 Defining structs There are several ways to define a struct in a C program For this course Define a new type specifying the fields in the struct Declare variables as needed using that new type The type is defined only once at the beginning of the program Variables with this new type can be declared as needed S 7 Defining struct types typedef struct int account int age char sex double premium account record record for one policy account number policy holder s age policy holder s sex monthly premium Defines a new data type called account record Does not declare create any variables No storage is allocated S 8 Style Points in struct types In a type definition use comments to describe the fields not the contents of the fields for any particular variable I e describe the layout of an account record not information about Alice s account typedefs normally are placed at the top of the program file S 9 Declaring struct Variables typedef students record goes at top of program account record alice account record bob account record is a type alice and bob are variables Both variables have the same internal layout alice account age sex premium bob account age sex premium S 11 Field access A fundamental operation on struct variables is field access struct name field name selects the given field variable from the struct alice alice age 23 alice premium 12 20 alice premium 2 alice premium account age 23 sex 12 20 24 40 premium S 12 Field access A selected field is an ordinary variable it can be used in all the usual ways alice account age sex premium alice age printf Alice is d years old n alice age scanf lf alice premium S 13 Terminology The terms struct record and structure mean the same thing fields are often called components or members S 14 Why use structs Collect together values that are treated as a unit for compactness readability maintainability typedef struct int dollars cents money typedef struct int hours minutes double seconds time This is an example of abstraction S 15 Structs as User Defined Types C provides a limited set of built in types int char double and variants of these not discussed in these lectures Pointers introduced some new types Arrays further enrich the possible types available But the objects in the real world and in computer applications are often more complex than these types allow With structs we re moving toward a way for programmers to define their own types S 16 Some Limitations Like arrays there are some restrictions on how a struct can be used compared to a simple variable int double etc Can t compare two structs directly Can t read or write an entire struct with scanf printf But you can do these things on individual fields S 17 struct Assignment Unlike arrays entire structs can be copied in a single operation Don t need to copy field byfield Can assign struct values with Can have functions with struct result types and can use struct values in a return statement S 18 struct Assignment dilbert A struct assignment copies all of the fields If dilbert is another account record then dilbert bob is equivalent to dilbert account bob account dilbert age bob age dilbert sex bob sex dilbert premium bob premium account age sex premium 46532 12 m 12 95 bob account age sex premium 46532 12 m 12 95 S 19 structs as Parameters structs behave like all other non array values when used as function parameters Can be call by value copied Can use as pointer parameters S 20 struct initializers A struct can be given an initial value when it is declared List initial values for the fields in the same order they appear in the struct typedef account record ratbert 970142 6 99 95 S 21 Midpoint Example Revisited Given 2 endpoints of a line return coordinates of midpoint void midpoint double x1 double y1 double x2 double y2 double midxp double midyp midxp x1 x2 2 0 midyp y1 y2 2 0 x1 y1 double ax ay bx by mx my midpoint ax ay bx by mx my x2 y2 x1 x2 y1 y2 2 2 S 22 Points as structs Better use a struct to make the concept of a point explicit in the code typedef struct representation of a point double x y x and y coordinates point point a 0 0 0 0 b 5 0 10 0 point m m x a x b x 2 0 m y a y b y 2 0 S 23 Midpoint with points return point whose coordinates are the center of the line segment with endpoints pt1 and pt2 point midpoint point pt1 point pt2 point mid mid x pt1 x pt2 x 2 0 mid y pt1 y pt2 y 2 0 return mid point a 0 0 0 0 b 5 0 10 0 m S 24 struct declaration and initialization m midpoint a b struct assignment Execution point midpoint point pt1 point pt2 point mid mid x pt1 x pt2 x 2 0 mid y pt1 y pt2 y 2 0 return mid point a 0 0 0 0 b 5 0 10 0 m m midpoint a b x 0 0 x 5 0 x 2 5 y 0 0 y 10 0 y 5 0 pt1 pt2 mid midpoint x 0 0 x 5 0 x 2 5 y 0 0 y 10 0 y 5 0 a b S 25 main m Midpoint with Pointers Instead of creating a temporary variable and returning a copy of it we could write the function so it stores the midpoint coordinates directly in the …


View Full Document

UW CSE 142 - Data Structures

Loading Unlocking...
Login

Join to view Data Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Structures and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?