1 / 10

16.216 ECE Application Programming

16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2011 Lecture 33: Structures. Lecture outline. Announcements/reminders Program 7 grading to be completed soon Program 8 due Friday, 12/9 Today Review: File input/output Structures Program 8 overview.

ledell
Download Presentation

16.216 ECE Application 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. 16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 33: Structures

  2. Lecture outline • Announcements/reminders • Program 7 grading to be completed soon • Program 8 due Friday, 12/9 • Today • Review: File input/output • Structures • Program 8 overview ECE Application Programming: Lecture 33

  3. Review: File I/O • Open file: FILE *fopen(filename, file_access) • Close file: fclose(file_handle) • Formatted I/O: • fprintf(file_handle, format_specifier, 0+ variables) • fscanf(file_handle, format_specifier, 0+ variables) • Unformatted I/O: • size_tfwrite(pointer, element size, # elements, file_handle) • size_tfread(pointer, element size, # elements, file_handle) • Check for EOF using either fscanf() result or feof(FILE *) ECE Application Programming: Lecture 33

  4. Structures • Arrays: groups of data with same type • Structures: groups of data with (potentially) different types • Example: record to store information about student: • First name (char []) • Middle initial (char) • Last name (char []) • ID # (unsigned int) • GPA (double) • Any data type—scalar, array, pointer (even other structures) allowed ECE Application Programming: Lecture 33

  5. Declaring structure types • Can define structure as a type using typedef • Could omit typedef, but would need “struct” before type name • Syntax: typedefstruct { <list of variables> } <typeName>; • Example: typedefstruct { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; } StudentInfo; • typedef usually at program start (with #include, #define) • <typeName> usually starts with capital letter ECE Application Programming: Lecture 33

  6. Using structure types • Once defined, can declare variables using that type • Scalar: StudentInfo student1; • Array: StudentInfo classList[10]; • Pointer: StudentInfo *sPtr; ECE Application Programming: Lecture 33

  7. Using structure variables • Initialization very similar to array initialization: StudentInfo student1 = { “John”, ‘Q’, “Smith”, 12345678, 3.75 }; • Accessing structure elements: . operator • Syntax: <var name>.<element name> • Examples: • printf(“%s %c %s”, student1.first, student1.middle, student1.last); • student1.GPA = 3.5; ECE Application Programming: Lecture 33

  8. Example 1: Using structures • What does the following print? typedefstruct { double real; double imag; } Complex; int main() { Complex a = {1, 2}; Complex b = {3.4, 5.6}; Complex c, d, e; printf("A = %.2lf+%.2lfi\n", a.real, a.imag); printf("B = %.2lf+%.2lfi\n", b.real, b.imag); c = a; d.real = a.real + b.real; d.imag = a.imag + b.imag; e.real = a.real - b.real; e.imag = a.imag - b.imag; printf("C = %.2lf+%.2lfi\n", c.real, c.imag); printf("D = %.2lf+%.2lfi\n", d.real, d.imag); printf("E = %.2lf+%.2lfi\n", e.real, e.imag); return 0; } ECE Application Programming: Lecture 33

  9. Example 1 solution A = 1.00+2.00i B = 3.40+5.60i C = 1.00+2.00i D = 4.40+7.60i E = -2.40+-3.60i ECE Application Programming: Lecture 33

  10. Next time • More on structures • Course evaluations ECE Application Programming: Lecture 33

More Related