SPEAKER 1: Let's write a program that prompts the user for the names and houses of three students. Now, to store those names and houses we could use, what, six variables. Three strings for the names, and another three strings for the houses, but we already know that we can clean up that code by using arrays instead, for instance an array of size 3 for the three names, and another array of size 3 for the houses. But it turns out we can clean this up further still, and actually keep those names and houses together, so that a student's name and his or her house are somehow encapsulated, so to speak, into the same variable. To do this, though, we need to declare our own data type, our own type in C, that the authors of C didn't necessarily think of years ago. To do this we can use the keyword typedef, along with the other keyword struct. Let's take a look. Inside of structs.h, I've already gotten started by including the CS50 library. I'm next going to type typedef struct, and then a curly brace. Inside of the struct, I'm going to specify that a student shall have a string called name, and another string called house. I'm going to then close my curly braces, and specify that the name of this new data type shall be student. In other words, via this syntax have I declared a new data type of my own that didn't exist a moment ago, and inside of this data type are two data fields or data members, one called name, one called house, both of which are of type string. Let's now use this type in an actual program. In structs0.c, I've similarly gotten myself started already with some boilerplate code, and I'm now going to use this data type student, as follows. I'm first going to declare an array of type student, I'll call the array students-- plural-- and I'll specify that its size will be three, which, notice, is the value of the constant STUDENTS-- in all capitals-- that I've declared up here, earlier in the file. Let's now iterate over those three students, and prompt the user for their names and houses. for int i get 0, i is less than that constant, i++. And now inside of the body of this for loop, I'm going to print out something like student's name. I'm then going to actually get that student's name by specifying students bracket i. In other words I want the i-th student in the array called students, but now I want to get at that i-th student's name, and to do this, I'm going to use the . operator, in order to get at a specific field inside of the struct. So I specify students bracket i .name gets the return value of GetString(). Meanwhile, I'm going to print out something similar, saying student's house, and now I'm going to specify that the i-th students house field shall get the return value of another call to GetString(). Now let's do something with these three students' names and houses, something simple like printing each out in a sentence. for int i get 0, again i is less than students, i++, printf "%s is in %s . backslash n", and now let me plug in the values of those two fields, students bracket i .name, comma, students bracket i .house, close paren, semicolon. And now I need to do one more thing. At the bottom of this file, I need to free the memory that was allocated behind the scenes by GetSring(), which of course calls malloc, in order to allocate memory for the strings the user types. But this to is simple. for int i get 0, i is less than students, i++, and inside the body of this for loop, I'm simply going to provide free students bracket i .name, and free students bracket i .house. Now, we've clearly used three for loops in this program, when really I could have just used one, but this is just for demonstration's sake, so that we can specify in three distinct steps what exactly we're doing. We're first getting a name and a house for each of the students, we're then printing out the name and the house for each of the three students, and then we're going to free the memory used by each of the students. But surely we could've combine this into one bigger for loop. Let's now save, compile, and run this program. make structs 0 ./structs 0, student's name, let's provide David, he'll live in Mather House, student's name, let's say Lauren, she'll live in Leverett House, student's name, Rob, he'll live in Kirkland House. And indeed, David is in Mather, Lauren is in Leverett, and Rob is in Kirkland.