70 likes | 83 Views
This lecture covers the review of structures and pointers in application programming. It includes topics such as nested structures and user-defined types. The lecture also provides an exercise to complete specified functions for the Name and StudentInfo structures.
E N D
EECE.2160ECE Application Programming Instructors: Dr. Michael Geiger & Peilong Li Spring 2016 Lecture 27: PE4: Structures
Lecture outline • Announcements/reminders • All remaining regrade deadlines: end of semester (4/29) • Program 7 due 4/11 • Only 9 programs this term • Tentative due dates: 4/20 (P8) and 4/29 (P9) • All 9 programs will count toward your grade • Lowest score will notbe dropped • Review • Structures and pointers • Structures and functions • Nested structures • Today’s class • Finish structures ECE Application Programming: Lecture 27
Review: Structures • User-defined types; example: typedef struct { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; } StudentInfo; • Can define variables of that type • Scalar: StudentInfo student1; • Array: StudentInfo classList[10]; • Pointer: StudentInfo *sPtr = &student1; • Access members using • Dot operator: student1.middle = ‘J’; • Arrow (if pointers): sPtr->GPA = 3.5; • Typically passed to functions by address ECE Application Programming: Lecture 27
Review: Nested structures • Structures can contain other structures: typedefstruct { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name } Name; typedefstruct { Name sname; // Student name unsigned int ID; // ID # double GPA; // Grade point } StudentInfo; • Will need multiple dot operators to access field within nested structure • Given StudentInfo s1; • s1.sname Name structure within s1 • s1.sname.middle middle initial of name within s1 ECE Application Programming: Lecture 27
Today’s exercise • Given header files, main program • Complete specified functions • For the Name structure • void printName(Name *n): Print the name pointed to by n, using format <first> <middle>. <last> • void readName(Name *n): Prompt for and read a first, middle, and last name, and store them in the structure pointed to by n • StudentInfo functions on next slide ECE Application Programming: Lecture 27
Today’s exercise (continued) • Given header files, main program • Complete specified functions • Name functions on previous slide • For the StudentInfo structure • void printStudent(StudentInfo *s): Print information about the student pointed to by s • void readStudent(StudentInfo *s): Prompt for and read information into the student pointed to by s • void printList(StudentInfo list[], int n): Print the contents of an array list that contains nStudentInfo structures • intfindByLName(StudentInfo list[], int n, char lname[]): Search for the student with last name lname in the array list. Return the index of the structure containing that last name, or -1 if not found • intfindByID(StudentInfo list[], int n, unsigned intsID): Search for the student with ID # sID in the array list. Return the index of the structure containing that last name, or -1 if not found ECE Application Programming: Lecture 27
Next time • Finish PE4 • Dynamic memory allocation • Reminders: • Program 7 due 4/11 • Only 9 programs this term • Tentative due dates: 4/20 (P8) and 4/29 (P9) • All 9 programs will count toward your grade • Lowest score will notbe dropped ECE Application Programming: Lecture 27