100 likes | 204 Views
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.
E N D
16.216ECE 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 ECE Application Programming: Lecture 33
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
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
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
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
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
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
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
Next time • More on structures • Course evaluations ECE Application Programming: Lecture 33