EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2018 Lecture 30 Structures review
ECE Application Programming: Lecture 30 Lecture outline Announcements/reminders Program 5 regrades due today Program 6 late penalties—last day for -1 penalty Late penalties capped at -1 between Tuesday, 11/20 and Monday, 11/26 Penalties begin increasing again Tuesday, 11/27 Program 7 due Tuesday, 12/4 Today’s lecture: Structures ... again An attempt to tie together everything we’ve covered about structures into one lecture 7/16/2019 ECE Application Programming: Lecture 30
Working with structures Can rarely work with entire structure Assignment only—that’s it Work with individual member(s) Must eventually deal with built-in types Different modes of access for Single structure variable dot operator Pointer to structure variable arrow operator Let’s discuss how and why … 7/16/2019 ECE Application Programming: Lecture 30
ECE Application Programming: Lecture 30 Structures vs. Arrays Arrays Pro: simple access (location- or index-based) Con: doesn’t support multiple data types Structures Pro: more flexibility in data types Con: complex access to members (name-based) Complexity of access due to memory layout 7/16/2019 ECE Application Programming: Lecture 30
ECE Application Programming: Lecture 30 Dot operator Used with single structure variable Must specify specific instance of new data type General form: <var name>.<member name> Examples (to be handwritten): 7/16/2019 ECE Application Programming: Lecture 30
ECE Application Programming: Lecture 30 Pointers Why are pointers useful? Primarily as pointer arguments Allow function to modify data declared outside Why else are structure pointers useful? Hint: how are arrays always passed to functions? Saves time and space to pass structs by address Use arrow operator—does work of Pointer dereferencing Member selection (dot operator) 7/16/2019 ECE Application Programming: Lecture 30
ECE Application Programming: Lecture 30 Nested structures Structures can contain other structures: typedef struct { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name } Name; Name sname; // Student name unsigned int ID; // ID # double GPA; // Grade point } SINew; 7/16/2019 ECE Application Programming: Lecture 30
Nested structure accesses Will need multiple dot operators to access field within nested structure Given SINew s1; s1.sname Name structure within s1 s1.sname.middle middle initial of name within s1 Structure pointer typically only to top-level structure Given SINew *sp = &s1; sp->sname Name structure within s1 sp->sname.middle middle initial of name within s1 7/16/2019 ECE Application Programming: Lecture 30
ECE Application Programming: Lecture 30 Final notes Next time PE3: Structures (no, really) Reminders: Program 5 regrades due today Program 6 late penalties—last day for -1 penalty Late penalties capped at -1 between Tuesday, 11/20 and Monday, 11/26 Penalties begin increasing again Tuesday, 11/27 Program 7 due Tuesday, 12/4 7/16/2019 ECE Application Programming: Lecture 30