{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture20-mar9

# lecture20-mar9 - Announcements Lecture 20 Read Chapter 14...

This preview shows pages 1–2. Sign up to view the full content.

Announcements Lecture 20 Read Chapter 14 for after break Assignment 4 – Due Friday – Write down your stats: SLOC , hrs., logic defects Friday lecture and recitations cancelled Topics for today (Ch. 16) – Structures – Bit fields – Unions – Enumerations Structures Aggregation of heterogeneous variables (elements) under one name The declaration forms a template (a new data type) that can be used to create variables of that type, e.g. struct Person /* Define a structure type */ { char firstName[32]; char lastName[32]; int age; } ; Now use the above type to create variables of that type struct Person student1; /* define struct variable of that type */ struct Person student2; /* define struct variable of that type */ Now assign values to the elements of a struct variable - use Dot notation strcpy ( student1 . firstName , ”Harry" ); student1.lastName = “Hacker”; student1 . age = 19; student ; /* declare a variable another way */ Arrays of Structures #define CLASS_SIZE 142 struct Person_record { char firstName[32]; char lastName[32]; int age; }; typedef Person struct Person_record ; /* Define Person type alias */ Person students [ CLASS_SIZE ] ; /* define an array of Persons */ . . . /* assign a particular student’s attribute values */ strcpy( students [3] . firstName, "John" ); strcpy( students [3] . lastName, "Doe" ); students [3] . age = 20; /* or similarly */ int i =3; strcpy( students [i] . firstName, "John" ); strcpy( students [i] . lastName, "Doe" ); students [i] . age = 20; Arrays of Structures Person students [ CLASS_SIZE ] ; . . . /* fill up the students array from user input */ int i = 0; printf (“input each students information, type in QUIT when done:\n”); do { printf (“input a student\’s first name, last name and age separated \ by spaces:\n”); scanf (“%s %s %d”, students [i].firstName, students [i].lastName, students [i].age); i++; } while (!strcmp(students[i].firstName, “QUIT”) && (i < CLASS_SIZE))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern