Homework Exam - PowerPoint PPT Presentation

About This Presentation
Title:

Homework Exam

Description:

What can we do with a struct? Reference members. box.pt2.x = box.pt1.x width; Assign as a unit. pt2 = pt1; Create a pointer to it. struct point *ppt1; ppt1 = &pt1; ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 15
Provided by: bobw76
Learn more at: https://www.cs.umb.edu
Category:
Tags: assign | exam | homework

less

Transcript and Presenter's Notes

Title: Homework Exam


1
Homework / Exam
  • Continuing KR Chapter 6
  • Exam 2 after next class
  • Open Book / Open Notes
  • Up through end of KR 6.4 plus MAKE

2
Review structs
  • struct point
  • int x
  • int y
  • pt
  • struct point pt1, pt2
  • struct point maxpt 320, 200

3
Review structs
  • struct rect
  • struct point pt1
  • struct point pt2
  • ? Note must have following
  • struct rect box

4
Review structs
  • box.pt1.x 5
  • box.pt1.y 10
  • box.pt2.x 10
  • box.pt2.y 20
  • area (box.pt2.x box.pt1.x)
  • (box.pt2.y box.pt1.y)

5
What can we do with a struct?
  • Reference members
  • box.pt2.x box.pt1.x width
  • Assign as a unit
  • pt2 pt1
  • Create a pointer to it
  • struct point ppt1
  • ppt1 pt1

6
What can we do with a struct?
  • Not legal to compare structs
  • if (pt1 pt2) ? INVALID
  • Must be done as
  • if (pt1.x pt2.x pt1.y pt2.y)

7
structs and Functions, KR 6.2
  • / ptinrect
  • if point p in rect r, return 1 else return 0
  • note slightly different from KR example /
  • int ptinrect (struct point p, struct rect r)
  • return p.x gt r.pt1.x p.x lt r.pt2.x
  • p.y gt r.pt1.y p.y lt r.pt2.y

8
Arrays of structs, KR 6.3
  • Multiple related arrays
  • char keywordNKEYS
  • int keycountNKEYS
  • Can be implemented as an array of structs
  • struct key
  • char word
  • int count
  • keytab NKEYS

9
Arrays of structs
  • Alternative array of structs implementation
  • struct key
  • char word
  • int count
  • struct key keytabNKEYS

10
Arrays of structs
  • Initialization for an array of structs
  • struct key
  • char word
  • int count
  • keytab
  • auto, 0,
  • while, 0
  • / NKEYS is dynamically derived /

11
Size of structs
  • sizeof is a compile-time unary operator
  • Can be used to get integer (actually size_t)
  • sizeof object
  • sizeof (type name)
  • Applied to structs
  • define NKEYS (sizeof keytab / sizeof (struct
    key))
  • define NKEYS (sizeof keytab / sizeof keytab0)

12
Pointers to structs, KR 6.4
  • Declare and initialize a pointer to struct
  • struct point p
  • struct point pp p
  • Refer to members of struct p via pointer pp
  • (pp).x and (pp).y ? See precedence
  • More commonly done as
  • pp-gtx and pp-gty ? See precedence

13
Pointers to structs
  • struct string
  • int len
  • char cp
  • p
  • Expression Same as Value / Effect
  • p-gtlen (p-gtlen) increments len
  • p-gtcp (p-gtcp) value is a char
  • p-gtcp ((p-gtcp)) value is a char
  • increments cp

14
Pointers to structs
  • / ptinrect (pointer version)
  • if point p in rect r, return 1 else return 0 /
  • int ptinrect (struct point pp, struct rect rp)
  • return pp-gtx gt rp-gtpt1.x pp-gtx lt rp-gtpt2.x
  • pp-gty gt rp-gtpt1.y pp-gty lt rp-gtpt2.y
Write a Comment
User Comments (0)
About PowerShow.com