SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Structures in ECE Application Programming

ledell avatar
10 diapositives · 235 Views · Aug 23, 2014

Learn about structures in programming, including file I/O review, declaring structure types, using structure variables, and examples.

Understanding Structures in ECE Application Programming - slide 1
01 / 10Understanding Structures in ECE Application Programming
1 / 17

Télécharger la présentation

Understanding Structures in ECE Application Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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