2 00:00:00,000 --> 00:00:02,760 >> SPEAKER 1: Let's write a program that prompts the user for the names and 3 00:00:02,760 --> 00:00:04,700 houses of three students. 4 00:00:04,700 --> 00:00:08,840 Now, to store those names and houses we could use, what, six variables. 5 00:00:08,840 --> 00:00:12,260 Three strings for the names, and another three strings for the houses, 6 00:00:12,260 --> 00:00:16,560 but we already know that we can clean up that code by using arrays instead, 7 00:00:16,560 --> 00:00:20,220 for instance an array of size 3 for the three names, and another array of 8 00:00:20,220 --> 00:00:22,110 size 3 for the houses. 9 00:00:22,110 --> 00:00:25,870 But it turns out we can clean this up further still, and actually keep those 10 00:00:25,870 --> 00:00:30,520 names and houses together, so that a student's name and his or her house 11 00:00:30,520 --> 00:00:34,940 are somehow encapsulated, so to speak, into the same variable. 12 00:00:34,940 --> 00:00:39,095 >> To do this, though, we need to declare our own data type, our own type in C, 13 00:00:39,095 --> 00:00:42,660 that the authors of C didn't necessarily think of years ago. 14 00:00:42,660 --> 00:00:45,630 To do this we can use the keyword typedef, along with the 15 00:00:45,630 --> 00:00:47,200 other keyword struct. 16 00:00:47,200 --> 00:00:48,160 Let's take a look. 17 00:00:48,160 --> 00:00:50,650 Inside of structs.h, I've already gotten started by 18 00:00:50,650 --> 00:00:52,560 including the CS50 library. 19 00:00:52,560 --> 00:00:57,640 I'm next going to type typedef struct, and then a curly brace. 20 00:00:57,640 --> 00:01:01,370 Inside of the struct, I'm going to specify that a student shall have a 21 00:01:01,370 --> 00:01:04,960 string called name, and another string called house. 22 00:01:04,960 --> 00:01:08,430 I'm going to then close my curly braces, and specify that the name of 23 00:01:08,430 --> 00:01:11,420 this new data type shall be student. 24 00:01:11,420 --> 00:01:15,550 In other words, via this syntax have I declared a new data type of my own 25 00:01:15,550 --> 00:01:19,910 that didn't exist a moment ago, and inside of this data type are two data 26 00:01:19,910 --> 00:01:24,270 fields or data members, one called name, one called house, both of which 27 00:01:24,270 --> 00:01:25,630 are of type string. 28 00:01:25,630 --> 00:01:27,690 >> Let's now use this type in an actual program. 29 00:01:27,690 --> 00:01:30,210 30 00:01:30,210 --> 00:01:34,090 In structs0.c, I've similarly gotten myself started already with some 31 00:01:34,090 --> 00:01:36,999 boilerplate code, and I'm now going to use this data 32 00:01:36,999 --> 00:01:39,100 type student, as follows. 33 00:01:39,100 --> 00:01:42,450 I'm first going to declare an array of type student, I'll 34 00:01:42,450 --> 00:01:43,920 call the array students-- 35 00:01:43,920 --> 00:01:44,630 plural-- 36 00:01:44,630 --> 00:01:49,360 and I'll specify that its size will be three, which, notice, is the value of 37 00:01:49,360 --> 00:01:50,880 the constant STUDENTS-- 38 00:01:50,880 --> 00:01:51,890 in all capitals-- 39 00:01:51,890 --> 00:01:54,930 that I've declared up here, earlier in the file. 40 00:01:54,930 --> 00:01:58,280 Let's now iterate over those three students, and prompt the user for 41 00:01:58,280 --> 00:02:00,050 their names and houses. 42 00:02:00,050 --> 00:02:05,422 >> for int i get 0, i is less than that constant, i++. 43 00:02:05,422 --> 00:02:08,600 And now inside of the body of this for loop, I'm going to print out something 44 00:02:08,600 --> 00:02:11,470 like student's name. 45 00:02:11,470 --> 00:02:14,890 I'm then going to actually get that student's name by specifying 46 00:02:14,890 --> 00:02:17,290 students bracket i. 47 00:02:17,290 --> 00:02:21,550 In other words I want the i-th student in the array called students, but now 48 00:02:21,550 --> 00:02:25,340 I want to get at that i-th student's name, and to do this, I'm going to use 49 00:02:25,340 --> 00:02:30,160 the . operator, in order to get at a specific field inside of the struct. 50 00:02:30,160 --> 00:02:37,100 So I specify students bracket i .name gets the return value of GetString(). 51 00:02:37,100 --> 00:02:40,310 Meanwhile, I'm going to print out something similar, saying student's 52 00:02:40,310 --> 00:02:45,410 house, and now I'm going to specify that the i-th students house field 53 00:02:45,410 --> 00:02:49,480 shall get the return value of another call to GetString(). 54 00:02:49,480 --> 00:02:52,350 >> Now let's do something with these three students' names and houses, 55 00:02:52,350 --> 00:02:56,230 something simple like printing each out in a sentence. 56 00:02:56,230 --> 00:03:07,580 for int i get 0, again i is less than students, i++, printf "%s is in %s . 57 00:03:07,580 --> 00:03:12,600 backslash n", and now let me plug in the values of those two fields, 58 00:03:12,600 --> 00:03:19,055 students bracket i .name, comma, students bracket i .house, close 59 00:03:19,055 --> 00:03:21,290 paren, semicolon. 60 00:03:21,290 --> 00:03:23,020 >> And now I need to do one more thing. 61 00:03:23,020 --> 00:03:26,600 At the bottom of this file, I need to free the memory that was allocated 62 00:03:26,600 --> 00:03:30,290 behind the scenes by GetSring(), which of course calls malloc, in order to 63 00:03:30,290 --> 00:03:32,500 allocate memory for the strings the user types. 64 00:03:32,500 --> 00:03:35,720 But this to is simple. 65 00:03:35,720 --> 00:03:42,610 for int i get 0, i is less than students, i++, and inside the body of 66 00:03:42,610 --> 00:03:48,670 this for loop, I'm simply going to provide free students bracket i .name, 67 00:03:48,670 --> 00:03:52,300 and free students bracket i .house. 68 00:03:52,300 --> 00:03:55,620 >> Now, we've clearly used three for loops in this program, when really I 69 00:03:55,620 --> 00:03:58,800 could have just used one, but this is just for demonstration's sake, so that 70 00:03:58,800 --> 00:04:02,850 we can specify in three distinct steps what exactly we're doing. 71 00:04:02,850 --> 00:04:06,120 We're first getting a name and a house for each of the students, we're then 72 00:04:06,120 --> 00:04:08,900 printing out the name and the house for each of the three students, and 73 00:04:08,900 --> 00:04:11,910 then we're going to free the memory used by each of the students. 74 00:04:11,910 --> 00:04:15,310 But surely we could've combine this into one bigger for loop. 75 00:04:15,310 --> 00:04:18,650 >> Let's now save, compile, and run this program. 76 00:04:18,650 --> 00:04:26,850 make structs 0 ./structs 0, student's name, let's provide David, he'll live 77 00:04:26,850 --> 00:04:31,580 in Mather House, student's name, let's say Lauren, she'll live in Leverett 78 00:04:31,580 --> 00:04:36,590 House, student's name, Rob, he'll live in Kirkland House. 79 00:04:36,590 --> 00:04:39,440 And indeed, David is in Mather, Lauren is in Leverett, 80 00:04:39,440 --> 00:04:40,810 and Rob is in Kirkland. 81 00:04:40,810 --> 00:04:43,425