1 00:00:00,000 --> 00:00:05,115 2 00:00:05,115 --> 00:00:07,230 >> JASON HIRSCHHORN: Welcome to week four, every one. 3 00:00:07,230 --> 00:00:09,910 We have a busy week ahead of us. 4 00:00:09,910 --> 00:00:14,030 On Monday this week and on Wednesday last week, you've been introduced to 5 00:00:14,030 --> 00:00:17,920 pointers, and so hopefully that has blown your mind, but hopefully not 6 00:00:17,920 --> 00:00:18,870 made you too confused. 7 00:00:18,870 --> 00:00:23,140 If it has, we will go over that in section today as well as we're going 8 00:00:23,140 --> 00:00:24,510 to do pointers. 9 00:00:24,510 --> 00:00:28,090 We're going to go over the stack, which we've talked about in lecture 10 00:00:28,090 --> 00:00:32,150 already, and we're going to hint at things to come on Wednesday and beyond 11 00:00:32,150 --> 00:00:33,780 in terms of memory management. 12 00:00:33,780 --> 00:00:38,430 And finally, we're going to talk a bit about the Stanford Portable Language, 13 00:00:38,430 --> 00:00:41,920 which is the library you guys are using this week to implement the 14 00:00:41,920 --> 00:00:43,900 problem set. 15 00:00:43,900 --> 00:00:46,020 >> As I showed you all last week-- 16 00:00:46,020 --> 00:00:48,750 I wanted to put this slide up again, because this is a list of the 17 00:00:48,750 --> 00:00:51,680 resources that are available to you CS50. 18 00:00:51,680 --> 00:00:54,670 These are what we give you out of the box, and these are incredibly 19 00:00:54,670 --> 00:00:57,160 important for what the next slide will show. 20 00:00:57,160 --> 00:01:00,110 But before we get to the next slide, I want to go over these again. 21 00:01:00,110 --> 00:01:03,410 >> For every lecture, there are notes that are taken. 22 00:01:03,410 --> 00:01:06,850 It's a digest of the lecture, but it also has code snippets and helpful 23 00:01:06,850 --> 00:01:07,510 tips and tricks. 24 00:01:07,510 --> 00:01:11,140 So if you want to review lecture-- hopefully you didn't miss lecture, but 25 00:01:11,140 --> 00:01:13,760 if you did, you should rewatch it-- but when you want to review lecture, 26 00:01:13,760 --> 00:01:15,530 definitely check out those lecture notes. 27 00:01:15,530 --> 00:01:18,770 There's also source code online from every lecture. 28 00:01:18,770 --> 00:01:23,680 >> cs50.net/shorts are short, 6 to 12 minute videos. 29 00:01:23,680 --> 00:01:25,470 You guys have seen many of them. 30 00:01:25,470 --> 00:01:28,820 There are also videos we don't assign to you but cover topics that are 31 00:01:28,820 --> 00:01:30,310 covered in lecture in this course. 32 00:01:30,310 --> 00:01:33,550 So if you want to refresh on those topics, do check out these shorts. 33 00:01:33,550 --> 00:01:37,830 >> study.cs50.net has primers on a ton of topics. 34 00:01:37,830 --> 00:01:38,880 It has videos. 35 00:01:38,880 --> 00:01:39,930 It has practice problems. 36 00:01:39,930 --> 00:01:41,880 It is a fantastic resource. 37 00:01:41,880 --> 00:01:46,380 Come any questions, but also quiz time. 38 00:01:46,380 --> 00:01:47,390 >> A couple more resources. 39 00:01:47,390 --> 00:01:50,160 There is the man command in the terminal that gives you some 40 00:01:50,160 --> 00:01:52,590 information on a particular function or command. 41 00:01:52,590 --> 00:01:55,320 There's Google, my favorite resource-- one of my favorite resources. 42 00:01:55,320 --> 00:01:58,610 My favorite resource is the last one, which we'll get to in a second. 43 00:01:58,610 --> 00:02:00,520 There's cs50.net/discuss. 44 00:02:00,520 --> 00:02:01,930 That's our classes forum. 45 00:02:01,930 --> 00:02:05,080 There are office hours Mondays through Thursdays, 8:00 to 11:00. 46 00:02:05,080 --> 00:02:08,669 There are also online office hours for extension students. 47 00:02:08,669 --> 00:02:12,010 >> And finally, if you need anything, have any questions, comments, or 48 00:02:12,010 --> 00:02:14,240 concerns, you have my contact information. 49 00:02:14,240 --> 00:02:16,900 Get in touch with me, please. 50 00:02:16,900 --> 00:02:20,970 >> So as I hinted at, next Wednesday, a week from tomorrow, is your first 51 00:02:20,970 --> 00:02:25,180 quiz, quiz zero, because in computer science we start counting at zero. 52 00:02:25,180 --> 00:02:28,620 More information will be provided in lecture tomorrow, and a hand that will 53 00:02:28,620 --> 00:02:33,510 be posted eventually online with more specific details, in particular the 54 00:02:33,510 --> 00:02:34,860 location of quiz zero. 55 00:02:34,860 --> 00:02:37,390 But it'll take place next Wednesday instead of lecture. 56 00:02:37,390 --> 00:02:40,670 There's no lecture next Monday because it's a holiday. 57 00:02:40,670 --> 00:02:45,010 >> The quiz will cover all of the material from week zero to week five, 58 00:02:45,010 --> 00:02:47,420 so through this week, through lecture tomorrow. 59 00:02:47,420 --> 00:02:49,710 Material includes the lectures. 60 00:02:49,710 --> 00:02:51,580 Material includes sections. 61 00:02:51,580 --> 00:02:55,970 Material includes the problem sets, and especially those beginning parts 62 00:02:55,970 --> 00:02:58,590 of problem sets where you're walked through some background information on 63 00:02:58,590 --> 00:02:59,780 what you're about to cover. 64 00:02:59,780 --> 00:03:02,570 Do look through those problem sets. 65 00:03:02,570 --> 00:03:07,340 >> To help you all with the quiz, next Monday, even though it is a holiday, 66 00:03:07,340 --> 00:03:10,570 the course will be hosting a course-wide review session. 67 00:03:10,570 --> 00:03:13,465 Video of the course-wide review session will be posted online if you 68 00:03:13,465 --> 00:03:14,570 aren't able to make it. 69 00:03:14,570 --> 00:03:18,510 In addition, next Tuesday in section we're going to be doing a ton of quiz 70 00:03:18,510 --> 00:03:22,810 review and hopefully playing some jeopardy as well, but we're going to 71 00:03:22,810 --> 00:03:25,220 be going over answering all your questions, but I'm going to come with 72 00:03:25,220 --> 00:03:27,710 a lot of helpful questions and information based 73 00:03:27,710 --> 00:03:29,540 on 10 years of quizzes. 74 00:03:29,540 --> 00:03:31,780 I have a pretty good idea of what's going to show up and how questions are 75 00:03:31,780 --> 00:03:36,640 going to be asked, so we'll go over those in section next Tuesday. 76 00:03:36,640 --> 00:03:39,260 >> In addition, I didn't put this slide up last week, but this is something we 77 00:03:39,260 --> 00:03:40,030 did last week. 78 00:03:40,030 --> 00:03:42,960 This is how we approached a lot of the coding problems we did. 79 00:03:42,960 --> 00:03:46,710 I wanted to distill out our approach into some simple steps that can help 80 00:03:46,710 --> 00:03:49,580 you with solving a coding problem. 81 00:03:49,580 --> 00:03:53,710 >> Remember last week, we coded binary search, and the first thing we did 82 00:03:53,710 --> 00:03:55,820 when we wanted to code binary search is we thought about in our 83 00:03:55,820 --> 00:03:57,090 heads how it worked. 84 00:03:57,090 --> 00:04:00,400 We didn't draw a picture in class, but I asked everybody to watch the short 85 00:04:00,400 --> 00:04:03,250 video, which did have a picture representation of how 86 00:04:03,250 --> 00:04:04,900 binary search worked. 87 00:04:04,900 --> 00:04:07,800 The first and we really wanted to do to understand something is draw a 88 00:04:07,800 --> 00:04:09,900 picture to understand its logic, to make sure we know 89 00:04:09,900 --> 00:04:11,530 how this thing works. 90 00:04:11,530 --> 00:04:14,670 >> The next thing we did you in section last week was we took our mental 91 00:04:14,670 --> 00:04:17,100 model, and we turned it into some pseudocode. 92 00:04:17,100 --> 00:04:21,570 Pseudocode, again, programming-like syntax in words, but not C code. 93 00:04:21,570 --> 00:04:26,210 Not even specific to C. We just took our ideas and took the English we used 94 00:04:26,210 --> 00:04:31,510 to express it, and wrote it out in some sort of program-looking way. 95 00:04:31,510 --> 00:04:32,550 So that's just the logic part. 96 00:04:32,550 --> 00:04:34,010 We took care of our logic first. 97 00:04:34,010 --> 00:04:36,930 We broke up this hard programming problem into two parts, and we took 98 00:04:36,930 --> 00:04:38,670 care of the logic part first. 99 00:04:38,670 --> 00:04:40,520 >> And then we approached the syntax part. 100 00:04:40,520 --> 00:04:43,830 Remember after we drew up the pseudocode, we picked out parts of the 101 00:04:43,830 --> 00:04:47,840 pseudocode that we knew mapped onto specific elements in C. For example, 102 00:04:47,840 --> 00:04:53,050 for loops, if conditions, or greater than or equal to operators. 103 00:04:53,050 --> 00:04:56,100 And finally, the last thing we did, the very last thing we did, was 104 00:04:56,100 --> 00:04:58,640 actually code this program in C. 105 00:04:58,640 --> 00:05:02,100 >> We're going to go through this process again today in a relatively large 106 00:05:02,100 --> 00:05:05,620 coding problem that will take up the bulk of the second half of section, 107 00:05:05,620 --> 00:05:07,730 but I wanted to lay this framework out here for you guys. 108 00:05:07,730 --> 00:05:11,435 It is how we've been approaching problems in class, and how probably 109 00:05:11,435 --> 00:05:14,610 you've been approaching these problems on your own outside of section. 110 00:05:14,610 --> 00:05:16,990 So I'm going to leave this up here, and we'll refer back to this when 111 00:05:16,990 --> 00:05:21,930 we're coding later today and go systematically through these steps. 112 00:05:21,930 --> 00:05:23,320 >> Finally, feedback. 113 00:05:23,320 --> 00:05:27,670 If you have any feedback for me, the website is at top of this slide, 114 00:05:27,670 --> 00:05:29,670 sayat.me/cs50. 115 00:05:29,670 --> 00:05:30,460 Give me some feedback. 116 00:05:30,460 --> 00:05:33,320 I got some fantastic feedback from you guys last week. 117 00:05:33,320 --> 00:05:36,470 You said you really liked the pseudocoding and coding we did. 118 00:05:36,470 --> 00:05:41,250 Also somebody gave me a link to a study that has shown that waiting 119 00:05:41,250 --> 00:05:46,200 three to seven seconds after asking if anybody has questions is the optimal 120 00:05:46,200 --> 00:05:47,240 time to wait. 121 00:05:47,240 --> 00:05:50,850 So if you have your stopwatches out, today I'm going to try and wait seven 122 00:05:50,850 --> 00:05:56,240 seconds after I ask if anybody has any questions before moving on. 123 00:05:56,240 --> 00:05:59,340 >> So before we move on, does anybody have any questions so far? 124 00:05:59,340 --> 00:06:06,470 125 00:06:06,470 --> 00:06:07,810 >> Control room gave me seven seconds. 126 00:06:07,810 --> 00:06:11,480 OK, nobody had any questions, but feel free to ask them if you do. 127 00:06:11,480 --> 00:06:15,110 >> First, this week we are going to cover pointers, as we hinted at at the 128 00:06:15,110 --> 00:06:16,910 agenda at the beginning of class. 129 00:06:16,910 --> 00:06:18,060 Pointers you've seen in lecture. 130 00:06:18,060 --> 00:06:21,040 They have probably melted your mind. 131 00:06:21,040 --> 00:06:25,050 That's OK, that's what they've done to hundreds of CS50 students before you. 132 00:06:25,050 --> 00:06:29,900 So over the next 10 to 30 minutes, we are going to go through these pointers 133 00:06:29,900 --> 00:06:31,450 slowly and systematically. 134 00:06:31,450 --> 00:06:34,490 If at any point you have any questions, please ask me. 135 00:06:34,490 --> 00:06:38,430 I guarantee that your peers in this section and watching at home have the 136 00:06:38,430 --> 00:06:41,050 same exact questions. 137 00:06:41,050 --> 00:06:45,660 >> Let's talk generally first, before we dive into pointers, about variables 138 00:06:45,660 --> 00:06:47,900 and data, which we've been using before. 139 00:06:47,900 --> 00:06:50,800 In addition to having these things up on a slide, I'm going 140 00:06:50,800 --> 00:06:52,050 to draw these out. 141 00:06:52,050 --> 00:06:54,400 142 00:06:54,400 --> 00:07:00,140 >> As this slide says, data in memory has both a value and an address, so we're 143 00:07:00,140 --> 00:07:03,260 going to represent something in memory. 144 00:07:03,260 --> 00:07:06,460 Remember last week or when you've seen arrays being drawn, we generally draw 145 00:07:06,460 --> 00:07:10,850 them as series of boxes, so we'll just draw one variable as a box 146 00:07:10,850 --> 00:07:13,100 somewhere in memory. 147 00:07:13,100 --> 00:07:18,480 This box in this example, we'll call it x, and it has a value. 148 00:07:18,480 --> 00:07:23,700 In this case, we're initializing the variable to a value of 50. 149 00:07:23,700 --> 00:07:30,260 >> You'll also notice that in addition to just the value, this variable x has a 150 00:07:30,260 --> 00:07:31,570 location in memory. 151 00:07:31,570 --> 00:07:34,870 Indeed, as you are probably somewhat aware of, memory has a 152 00:07:34,870 --> 00:07:36,180 finite amount of spaces. 153 00:07:36,180 --> 00:07:39,490 There's only so many boxes inside your computer where you can store things. 154 00:07:39,490 --> 00:07:42,580 And better yet, so you know where things are stored, each box has a 155 00:07:42,580 --> 00:07:46,340 specific address, a specific number where you can find it. 156 00:07:46,340 --> 00:07:52,540 Just like freshmen all have their mailing address in the Science Center, 157 00:07:52,540 --> 00:07:55,070 every box in memory has a specific address. 158 00:07:55,070 --> 00:07:57,750 When you go to that address, you can get what's located there. 159 00:07:57,750 --> 00:08:02,300 160 00:08:02,300 --> 00:08:05,000 >> I'm going to be totally arbitrary with the address of this variable, but I'm 161 00:08:05,000 --> 00:08:07,450 going to say the address is 123. 162 00:08:07,450 --> 00:08:08,700 Now, I didn't just write 1-2-3. 163 00:08:08,700 --> 00:08:10,990 I wrote 0x123. 164 00:08:10,990 --> 00:08:15,280 The 0x part means this is a hexadecimal number. 165 00:08:15,280 --> 00:08:19,800 And we are not going to talk about hexadecimal a lot today, but note that 166 00:08:19,800 --> 00:08:23,750 when we refer to memory addresses, we refer to them in hexadecimal. 167 00:08:23,750 --> 00:08:27,000 So whenever I'm talking about an address, I'll put that 0x, and that's 168 00:08:27,000 --> 00:08:29,400 just saying 123 is a hexadecimal number. 169 00:08:29,400 --> 00:08:30,430 It's in base 16. 170 00:08:30,430 --> 00:08:32,760 It's not in base 10. 171 00:08:32,760 --> 00:08:34,270 >> Does anybody have any questions so far? 172 00:08:34,270 --> 00:08:42,090 173 00:08:42,090 --> 00:08:42,950 >> Perfect. 174 00:08:42,950 --> 00:08:44,430 So that's one variable. 175 00:08:44,430 --> 00:08:47,020 We've seen this before, but now we're starting to go under the hood and see 176 00:08:47,020 --> 00:08:48,350 what's actually happening. 177 00:08:48,350 --> 00:08:49,910 >> So, pointers. 178 00:08:49,910 --> 00:08:54,090 The cool thing about pointers is that unlike a normal variable that have 179 00:08:54,090 --> 00:08:57,500 some value, some integer, some character, maybe a series of 180 00:08:57,500 --> 00:09:01,930 characters if we're talking about an array, the value that a pointer has is 181 00:09:01,930 --> 00:09:04,290 an address. 182 00:09:04,290 --> 00:09:07,470 Up on the screen, we have the int x equals 50 that we created before. 183 00:09:07,470 --> 00:09:09,060 I've already drawn that box. 184 00:09:09,060 --> 00:09:12,090 >> Let's also draw a box. 185 00:09:12,090 --> 00:09:14,490 This is going to be-- oh, my goodness. 186 00:09:14,490 --> 00:09:15,810 We need a new marker. 187 00:09:15,810 --> 00:09:19,270 188 00:09:19,270 --> 00:09:19,845 Not much better. 189 00:09:19,845 --> 00:09:23,230 >> But we're going to call this box-- this is the variable called pointer. 190 00:09:23,230 --> 00:09:24,650 Pointer also has an address. 191 00:09:24,650 --> 00:09:26,180 It's in a different address. 192 00:09:26,180 --> 00:09:27,270 It's a different variable. 193 00:09:27,270 --> 00:09:28,820 It's not going to be on top of the first one. 194 00:09:28,820 --> 00:09:32,330 You can't take put two things inside of a mailbox. 195 00:09:32,330 --> 00:09:34,160 You can't overwrite the initial one, so we're going at 196 00:09:34,160 --> 00:09:35,200 different address here. 197 00:09:35,200 --> 00:09:37,880 We'll give it the address 456. 198 00:09:37,880 --> 00:09:42,190 But pointer's value is going to be an address, and in this case, it's going 199 00:09:42,190 --> 00:09:45,750 to be addressed of the x variable. 200 00:09:45,750 --> 00:09:50,000 So inside pointer, we're storing the address 123. 201 00:09:50,000 --> 00:09:52,700 >> And the syntax I use to write it-- we're going to go over the syntax in a 202 00:09:52,700 --> 00:09:56,420 second, but know that the syntax on this slide is the syntax you would use 203 00:09:56,420 --> 00:09:58,700 to set this situation up-- 204 00:09:58,700 --> 00:10:02,735 before we get to the syntax, though, I want to make clear to everybody and 205 00:10:02,735 --> 00:10:05,900 make sure everybody understands that pointers as distinct from variables 206 00:10:05,900 --> 00:10:11,560 have an address as their value, not an integer or a char. 207 00:10:11,560 --> 00:10:13,470 >> So far so good? 208 00:10:13,470 --> 00:10:16,360 >> Generally also, when we're drawing these things, I'm not just going to 209 00:10:16,360 --> 00:10:18,320 draw the address in here. 210 00:10:18,320 --> 00:10:24,100 I'm going to take an arrow from this box to the other variable that it's 211 00:10:24,100 --> 00:10:29,590 pointing to to give the sense that if I were to look at the address 123, if 212 00:10:29,590 --> 00:10:34,130 I were to use this pointer to find what it's pointing to, I would get 213 00:10:34,130 --> 00:10:35,150 this box over here. 214 00:10:35,150 --> 00:10:36,400 I would get the number 50. 215 00:10:36,400 --> 00:10:40,010 216 00:10:40,010 --> 00:10:41,810 >> Yes, question? 217 00:10:41,810 --> 00:10:46,200 >> STUDENT: If pointer stores an address, then how do we choose arrays, and int 218 00:10:46,200 --> 00:10:48,650 star, or like a char star, or whatever? 219 00:10:48,650 --> 00:10:50,190 >> JASON HIRSCHHORN: Can you repeat the middle part? 220 00:10:50,190 --> 00:10:51,310 I lost you. 221 00:10:51,310 --> 00:10:55,070 >> STUDENT: How do you choose arrays, like an int star or char star, or 222 00:10:55,070 --> 00:10:57,200 whatever, if it always stores an address? 223 00:10:57,200 --> 00:10:59,340 >> JASON HIRSCHHORN: Good question. 224 00:10:59,340 --> 00:11:01,730 How do we choose what it's actually pointing to if it 225 00:11:01,730 --> 00:11:03,010 always stores an address? 226 00:11:03,010 --> 00:11:07,840 If instead of writing int star right here, I wrote char star, it would be 227 00:11:07,840 --> 00:11:09,090 pointing to-- 228 00:11:09,090 --> 00:11:11,770 229 00:11:11,770 --> 00:11:14,750 that is what we would write if we wanted it to point to a character. 230 00:11:14,750 --> 00:11:19,720 Deep, deep down in the ones and zeros world, that int, that char, doesn't 231 00:11:19,720 --> 00:11:20,600 really matter. 232 00:11:20,600 --> 00:11:24,280 But in terms of C, it does want you to be pretty specific in terms of the 233 00:11:24,280 --> 00:11:27,660 type of thing you're pointing to. 234 00:11:27,660 --> 00:11:30,240 At the level at which we're programming, we're going to write the 235 00:11:30,240 --> 00:11:32,670 type of thing we're point to even though it's always an 236 00:11:32,670 --> 00:11:34,940 address inside this box. 237 00:11:34,940 --> 00:11:38,980 238 00:11:38,980 --> 00:11:40,420 Did you have a question? 239 00:11:40,420 --> 00:11:41,670 OK. 240 00:11:41,670 --> 00:11:43,240 241 00:11:43,240 --> 00:11:47,610 >> Now we're going to look at some syntax for pointers. 242 00:11:47,610 --> 00:11:50,070 The first line on this slide-- 243 00:11:50,070 --> 00:11:52,520 int star, space, pointer-- 244 00:11:52,520 --> 00:11:54,770 initializes and creates-- 245 00:11:54,770 --> 00:11:56,040 not initializes, rather. 246 00:11:56,040 --> 00:11:58,680 Doesn't set it equal to anything, but it creates this pointer. 247 00:11:58,680 --> 00:12:01,090 So in terms of my diagram-- 248 00:12:01,090 --> 00:12:03,790 let's go to this side-- 249 00:12:03,790 --> 00:12:08,510 that first line, int star pointer, creates a box. 250 00:12:08,510 --> 00:12:12,800 This box is the pointer variable, and it's at an address. 251 00:12:12,800 --> 00:12:16,250 We'll give it a different address, 999. 252 00:12:16,250 --> 00:12:17,010 >> That's all that line does. 253 00:12:17,010 --> 00:12:17,970 We've seen this before. 254 00:12:17,970 --> 00:12:21,750 That's like any time, if I were to just write int x there, it would do 255 00:12:21,750 --> 00:12:24,210 the same exact thing, create a box called x. 256 00:12:24,210 --> 00:12:29,350 In this case, we're giving it a different type. 257 00:12:29,350 --> 00:12:30,240 So it's not going to be an int. 258 00:12:30,240 --> 00:12:35,460 It's going to be an int star, and so this box will be a little bit 259 00:12:35,460 --> 00:12:38,650 different than if it were to just be a char, for example. 260 00:12:38,650 --> 00:12:41,550 >> Because chars, recall, are only one byte in memory. 261 00:12:41,550 --> 00:12:43,050 Pointers are four bytes. 262 00:12:43,050 --> 00:12:45,640 So this box will look a little bit different if we write int star as 263 00:12:45,640 --> 00:12:46,490 opposed to char. 264 00:12:46,490 --> 00:12:49,110 But other than that, we're creating a box, and it's at a specific address. 265 00:12:49,110 --> 00:12:51,830 That's all that first line does. 266 00:12:51,830 --> 00:12:57,630 >> The second line of code, int x equals 50, again creates another box x at a 267 00:12:57,630 --> 00:12:58,410 different location. 268 00:12:58,410 --> 00:13:00,340 We'll give dislocation 111. 269 00:13:00,340 --> 00:13:02,560 And this is actually initializing the pointer in addition to 270 00:13:02,560 --> 00:13:03,490 creating the box. 271 00:13:03,490 --> 00:13:07,380 We're putting a 50 inside this box. 272 00:13:07,380 --> 00:13:10,650 >> The third line of code, pointer equals address x. 273 00:13:10,650 --> 00:13:14,600 That address operator means a very specific thing. 274 00:13:14,600 --> 00:13:19,650 Whenever we put the ampersand in front of a variable name, we are saying give 275 00:13:19,650 --> 00:13:22,810 me the address of that variable. 276 00:13:22,810 --> 00:13:27,830 So when I write ampersand x, that's saying give me the address of x, in 277 00:13:27,830 --> 00:13:30,190 this case, 111. 278 00:13:30,190 --> 00:13:30,610 >> Yes? 279 00:13:30,610 --> 00:13:33,750 >> STUDENT: Would it be acceptable if we happen to know the address of this 280 00:13:33,750 --> 00:13:37,860 particular variable to just say pointer equals 0x111? 281 00:13:37,860 --> 00:13:41,130 >> JASON HIRSCHHORN: So if you knew that this was at 111, you could write 282 00:13:41,130 --> 00:13:46,825 pointer x equals this hexadecimal number, but I can't see a way that you 283 00:13:46,825 --> 00:13:50,880 would ever know what it is, so that's why we use this syntax. 284 00:13:50,880 --> 00:13:54,950 But again, that ampersand x, ampersand is saying give me the address of this 285 00:13:54,950 --> 00:13:59,960 variable, and just like any equal sign does, that's storing that 286 00:13:59,960 --> 00:14:01,210 value inside pointer. 287 00:14:01,210 --> 00:14:04,370 288 00:14:04,370 --> 00:14:07,760 >> So whenever you see that ampersand, think this is giving me the address of 289 00:14:07,760 --> 00:14:08,810 this thing. 290 00:14:08,810 --> 00:14:12,240 Ampersand x is giving me the address of this x box. 291 00:14:12,240 --> 00:14:16,990 Again, if we did ampersand pointer, what would I get? 292 00:14:16,990 --> 00:14:18,480 >> STUDENT: Address of the pointer. 293 00:14:18,480 --> 00:14:19,780 >> JASON HIRSCHHORN: The address of the pointer. 294 00:14:19,780 --> 00:14:20,410 What specifically? 295 00:14:20,410 --> 00:14:21,790 >> STUDENT: 0x999. 296 00:14:21,790 --> 00:14:23,870 >> JASON HIRSCHHORN: 999. 297 00:14:23,870 --> 00:14:26,920 Ampersand pointer would give me 999 in this case. 298 00:14:26,920 --> 00:14:28,730 Ampersand in front of any variable gives you the 299 00:14:28,730 --> 00:14:30,630 address of that variable. 300 00:14:30,630 --> 00:14:33,270 Of course, I don't really care about the address of this pointer. 301 00:14:33,270 --> 00:14:37,900 I care much more about the address of this variable. 302 00:14:37,900 --> 00:14:41,980 >> Finally, the last symbol we're going to see is actually the same symbol, 303 00:14:41,980 --> 00:14:43,170 that asterisk. 304 00:14:43,170 --> 00:14:45,910 But in this final line, it's used a bit differently. 305 00:14:45,910 --> 00:14:49,070 Notice how in this last line it's next to the variable. 306 00:14:49,070 --> 00:14:52,600 As before it was next to the type, it was part of this type, we're creating 307 00:14:52,600 --> 00:14:56,840 this type integer pointer, now the star means something 308 00:14:56,840 --> 00:14:57,510 a little bit different. 309 00:14:57,510 --> 00:15:00,700 It means go treat the value-- 310 00:15:00,700 --> 00:15:01,610 int pointer-- 311 00:15:01,610 --> 00:15:06,740 treat the value in this variable like an address, go to that address, and 312 00:15:06,740 --> 00:15:08,820 give me that value. 313 00:15:08,820 --> 00:15:15,430 >> So the star means, again, treat the value and pointer as an address, go to 314 00:15:15,430 --> 00:15:18,640 that address, and return that value. 315 00:15:18,640 --> 00:15:22,060 In this case, that value is 50, so this line would printf 50. 316 00:15:22,060 --> 00:15:22,430 Yes? 317 00:15:22,430 --> 00:15:25,770 >> STUDENT: So if we tried to do that on something that wasn't declared as a 318 00:15:25,770 --> 00:15:29,190 pointer, like on x, and the value is 50, would it try to go to address 50, 319 00:15:29,190 --> 00:15:30,535 or would it just give us an error? 320 00:15:30,535 --> 00:15:36,200 >> JASON HIRSCHHORN: If we did star x, and tried to printf that, what the 321 00:15:36,200 --> 00:15:39,780 compiler would do, or what the program would do when it was running, is it 322 00:15:39,780 --> 00:15:46,340 would treat 50 like an address and go to address number 50 in your program. 323 00:15:46,340 --> 00:15:51,980 Generally, when you're going to these random addresses, you're not going to 324 00:15:51,980 --> 00:15:54,310 go to an address where something is nice there. 325 00:15:54,310 --> 00:15:57,160 You're going to go to a really bad address that you're not supposed to go 326 00:15:57,160 --> 00:16:00,510 to, and your program is going to crash. 327 00:16:00,510 --> 00:16:01,560 >> So yes, you're exactly right. 328 00:16:01,560 --> 00:16:04,430 It would do the same exact thing, but when we try to go to addresses that we 329 00:16:04,430 --> 00:16:07,290 haven't asked for or we haven't declared, or that are just random 330 00:16:07,290 --> 00:16:10,360 addresses, we almost always get in trouble and our program crashes. 331 00:16:10,360 --> 00:16:14,020 332 00:16:14,020 --> 00:16:15,040 >> Did that answer your question? 333 00:16:15,040 --> 00:16:16,864 >> STUDENT: Yeah, I guess I have another one. 334 00:16:16,864 --> 00:16:20,460 So if you cold just do that and it would go to that address, what's the 335 00:16:20,460 --> 00:16:23,390 point of declaring a pointer as a pointer if we could just say a pointer 336 00:16:23,390 --> 00:16:24,610 equals ampersand thing? 337 00:16:24,610 --> 00:16:27,533 And then it would be like a meaningful address, and we could go there, right? 338 00:16:27,533 --> 00:16:29,130 >> JASON HIRSCHHORN: Can you repeat the question? 339 00:16:29,130 --> 00:16:32,210 >> STUDENT: Why do we have to declare the pointer as an int star if it would 340 00:16:32,210 --> 00:16:35,600 work to just declare it as like a regular int, because it would still 341 00:16:35,600 --> 00:16:39,336 have a thing that could be then dereferenced, right? 342 00:16:39,336 --> 00:16:42,490 >> JASON HIRSCHHORN: So why are we declaring it as an int star rather 343 00:16:42,490 --> 00:16:46,700 than just like an integer and saving this number in it? 344 00:16:46,700 --> 00:16:47,936 >> STUDENT: Yeah. 345 00:16:47,936 --> 00:16:50,100 >> JASON HIRSCHHORN: Because when we use things-- 346 00:16:50,100 --> 00:16:50,960 great question-- 347 00:16:50,960 --> 00:16:53,720 when we use things, whenever we're using a pointer and we're going to 348 00:16:53,720 --> 00:16:56,680 treat something like pointers, other things are going on that I'm not going 349 00:16:56,680 --> 00:17:00,080 to go into now that it helps if the program knows that 350 00:17:00,080 --> 00:17:01,270 this thing is a pointer. 351 00:17:01,270 --> 00:17:04,630 It will treat it differently in some other ways that is irrelevant for us, 352 00:17:04,630 --> 00:17:08,050 but it we are going to use it like a pointer, we should say we're going to 353 00:17:08,050 --> 00:17:09,300 be using this as a pointer. 354 00:17:09,300 --> 00:17:12,450 355 00:17:12,450 --> 00:17:17,584 >> So that is basically how pointers work. 356 00:17:17,584 --> 00:17:20,849 It's not much more complicated than that, but unfortunately, it becomes a 357 00:17:20,849 --> 00:17:23,710 lot more complicated when we put it into practice. 358 00:17:23,710 --> 00:17:28,190 >> Next, we are going to do something quite large. 359 00:17:28,190 --> 00:17:31,703 Instead of just throwing a couple lines of code online, I put up 4, 5, 360 00:17:31,703 --> 00:17:34,290 6, 7, 8 9, 10 lines of code. 361 00:17:34,290 --> 00:17:38,430 And rather than coding these out on your computer or running this program 362 00:17:38,430 --> 00:17:41,410 on your computer, I want you to take out a sheet of paper and a pencil or a 363 00:17:41,410 --> 00:17:45,800 pen, and figure out what each of the lines of these codes do. 364 00:17:45,800 --> 00:17:49,460 Start with the left column, working from top to bottom, which initializes 365 00:17:49,460 --> 00:17:55,040 there's a lot of these variables, and then go through each line of code in 366 00:17:55,040 --> 00:17:57,880 the right column and figure out what happens. 367 00:17:57,880 --> 00:18:01,890 I'll give everybody three minutes to start this on your own, and then we'll 368 00:18:01,890 --> 00:18:03,230 go over this as a group. 369 00:18:03,230 --> 00:20:10,620 370 00:20:10,620 --> 00:20:12,100 >> Let's get started going over this as a group. 371 00:20:12,100 --> 00:20:13,700 It's OK if you haven't finished. 372 00:20:13,700 --> 00:20:18,570 I'm going to start with the left hand side of the column, and then I'm going 373 00:20:18,570 --> 00:20:24,520 to invite you guys up here, the brave you amongst you up here, to execute 374 00:20:24,520 --> 00:20:26,730 one line of code on the right hand side the column, and then we'll work 375 00:20:26,730 --> 00:20:27,770 our way through. 376 00:20:27,770 --> 00:20:32,900 >> So first, that first line of code, int x equals 2 comma y equals 8 comma z 377 00:20:32,900 --> 00:20:36,500 equals 12, creates three variables. 378 00:20:36,500 --> 00:20:38,120 I did it all on one line. 379 00:20:38,120 --> 00:20:41,040 That's a handy syntax for creating multiple things of the 380 00:20:41,040 --> 00:20:44,030 same type on one line. 381 00:20:44,030 --> 00:20:46,000 >> So we have three boxes. 382 00:20:46,000 --> 00:20:48,834 383 00:20:48,834 --> 00:20:52,218 Can everybody see those three, or is that too low? 384 00:20:52,218 --> 00:20:53,152 OK. 385 00:20:53,152 --> 00:20:57,180 >> We have x, y, z. 386 00:20:57,180 --> 00:21:00,550 And in x, we get 2. 387 00:21:00,550 --> 00:21:03,035 In y, we get 8. 388 00:21:03,035 --> 00:21:07,390 and in z, we get 12. 389 00:21:07,390 --> 00:21:09,270 Pretty standard so far. 390 00:21:09,270 --> 00:21:12,430 >> Next we're creating another box, another variable. 391 00:21:12,430 --> 00:21:13,760 This is an int pointer. 392 00:21:13,760 --> 00:21:15,560 It's called pointer x. 393 00:21:15,560 --> 00:21:16,920 We'll draw that over here. 394 00:21:16,920 --> 00:21:22,640 395 00:21:22,640 --> 00:21:26,998 >> And what are we storing inside pointer x? 396 00:21:26,998 --> 00:21:29,290 >> STUDENT: The address of x. 397 00:21:29,290 --> 00:21:31,100 >> JASON HIRSCHHORN: The address of x. 398 00:21:31,100 --> 00:21:33,750 In this case, I don't know the address of x. 399 00:21:33,750 --> 00:21:35,970 It could be anything, but I'm going to make something up now to make it 400 00:21:35,970 --> 00:21:37,310 easier for us. 401 00:21:37,310 --> 00:21:40,360 We'll put x at 111. 402 00:21:40,360 --> 00:21:46,660 So this gets 0x111, and we'll give it a nice arrow to signify 403 00:21:46,660 --> 00:21:48,520 it's pointing to x. 404 00:21:48,520 --> 00:21:57,510 >> Likewise, I have pointer y and pointer z. 405 00:21:57,510 --> 00:21:58,580 They get values. 406 00:21:58,580 --> 00:22:18,672 We'll give this 0x222, 0x333. 407 00:22:18,672 --> 00:22:21,940 >> That's our initial setup on the left hand side of the column. 408 00:22:21,940 --> 00:22:25,510 We created three integers, and we created three pointers. 409 00:22:25,510 --> 00:22:28,290 Inside each pointer, we save the address of one of the three initial 410 00:22:28,290 --> 00:22:30,460 variables we created. 411 00:22:30,460 --> 00:22:35,710 >> Now, on the right hand side of the column, we are using that asterisk 412 00:22:35,710 --> 00:22:41,390 symbol in a number of different ways, not all of which are to use it to 413 00:22:41,390 --> 00:22:43,480 dereference something or to go to a specific address and 414 00:22:43,480 --> 00:22:45,720 return the value there. 415 00:22:45,720 --> 00:22:48,680 >> Can someone tell me what the first line of code does? 416 00:22:48,680 --> 00:22:51,360 417 00:22:51,360 --> 00:22:54,360 Somebody raise their hand who hasn't spoken yet. 418 00:22:54,360 --> 00:22:54,840 You? 419 00:22:54,840 --> 00:22:56,792 >> STUDENT: Set z to 16. 420 00:22:56,792 --> 00:23:00,340 >> JASON HIRSCHHORN: It's set z to 16. 421 00:23:00,340 --> 00:23:02,120 So z equals x times y. 422 00:23:02,120 --> 00:23:04,630 In that case, it's just multiplication we're doing. 423 00:23:04,630 --> 00:23:08,640 So z equals x times y, 2 times 8 is 16. 424 00:23:08,640 --> 00:23:09,080 Perfect. 425 00:23:09,080 --> 00:23:11,330 So we'll update this. 426 00:23:11,330 --> 00:23:14,010 z equal 16. 427 00:23:14,010 --> 00:23:18,270 >> That second line of code x space star equals space y. 428 00:23:18,270 --> 00:23:21,900 What does that line of code do? 429 00:23:21,900 --> 00:23:22,830 Jeff. 430 00:23:22,830 --> 00:23:24,470 >> STUDENT: x equals x times y? 431 00:23:24,470 --> 00:23:25,630 >> JASON HIRSCHHORN: Exactly right. 432 00:23:25,630 --> 00:23:28,870 That's shorthand notation for x equals x times y. 433 00:23:28,870 --> 00:23:32,030 You've probably seen things like x plus equals y? 434 00:23:32,030 --> 00:23:34,380 That's x equals x plus y. 435 00:23:34,380 --> 00:23:39,190 In this case, we're doing something a bit different, x equals x times y. 436 00:23:39,190 --> 00:23:40,400 >> Then what do I need to update here? 437 00:23:40,400 --> 00:23:41,915 >> STUDENT: x is 16. 438 00:23:41,915 --> 00:23:43,165 >> JASON HIRSCHHORN: x is 16. 439 00:23:43,165 --> 00:23:50,080 440 00:23:50,080 --> 00:23:52,840 >> Changes in green are what we have done so far. 441 00:23:52,840 --> 00:23:56,900 Next, we have y equal star pointer x. 442 00:23:56,900 --> 00:24:00,300 Can somebody walk me through what this line of code does? 443 00:24:00,300 --> 00:24:01,400 What does this line of code do? 444 00:24:01,400 --> 00:24:02,880 Walk me through from the beginning. 445 00:24:02,880 --> 00:24:07,060 >> STUDENT: It takes whatever pointer x is pointing to, the value that's being 446 00:24:07,060 --> 00:24:11,064 pointed by that, and it stores it in y. 447 00:24:11,064 --> 00:24:11,950 >> JASON HIRSCHHORN: Excellent. 448 00:24:11,950 --> 00:24:13,310 I'm going to repeat that again. 449 00:24:13,310 --> 00:24:18,180 You did a fantastic job, but so we can emphasize it, that star means treat 450 00:24:18,180 --> 00:24:20,550 the thing in that variable like an address and go to it. 451 00:24:20,550 --> 00:24:25,590 So, treat the thing in pointer x like an address, go to it, take that value 452 00:24:25,590 --> 00:24:28,750 16, and then we're just saving it in the variable y. 453 00:24:28,750 --> 00:24:34,393 So now y gets overwritten, and it is now 16. 454 00:24:34,393 --> 00:24:37,260 455 00:24:37,260 --> 00:24:42,810 >> The next line of code, star pointer underscore x equals x times y. 456 00:24:42,810 --> 00:24:44,400 What does that line of code do? 457 00:24:44,400 --> 00:24:47,120 458 00:24:47,120 --> 00:24:50,900 Again, if you're a little bit lost here, evaluate it from right to left. 459 00:24:50,900 --> 00:24:53,480 So Mondo, what does the right hand side of that do? 460 00:24:53,480 --> 00:24:55,820 >> STUDENT: The right hand side just multiplies whatever is in the 461 00:24:55,820 --> 00:24:56,990 box is x and y. 462 00:24:56,990 --> 00:24:58,880 >> JASON HIRSCHHORN: And that's 16 and 16, so what's that equal? 463 00:24:58,880 --> 00:24:59,350 >> STUDENT: 256. 464 00:24:59,350 --> 00:25:00,470 >> JASON HIRSCHHORN: 256. 465 00:25:00,470 --> 00:25:01,865 And where are we saving 256? 466 00:25:01,865 --> 00:25:05,410 >> STUDENT: You're saving it in whatever pointer x is pointing to, 467 00:25:05,410 --> 00:25:06,820 which is the x box. 468 00:25:06,820 --> 00:25:08,802 So now x becomes 256. 469 00:25:08,802 --> 00:25:11,480 >> JASON HIRSCHHORN: So the star, again, means go to. 470 00:25:11,480 --> 00:25:13,320 We go to what pointer x is pointing to. 471 00:25:13,320 --> 00:25:27,440 It's pointing to x, and there we're saving the value 256. 472 00:25:27,440 --> 00:25:29,750 >> Any questions about that specific one? 473 00:25:29,750 --> 00:25:33,860 We haven't seen the star used on the left hand side, yet, of 474 00:25:33,860 --> 00:25:35,110 an assignment operator. 475 00:25:35,110 --> 00:25:42,690 476 00:25:42,690 --> 00:25:44,410 >> So that's the fourth line of code. 477 00:25:44,410 --> 00:25:47,970 The fifth line of code, can somebody who hasn't spoken yet give me the 478 00:25:47,970 --> 00:25:49,710 fifth line of code, or walk through it with me? 479 00:25:49,710 --> 00:25:55,120 480 00:25:55,120 --> 00:25:55,950 Annie, what's it do? 481 00:25:55,950 --> 00:25:59,535 >> STUDENT: Change the value of pointer x to the value of pointer y. 482 00:25:59,535 --> 00:26:01,980 >> JASON HIRSCHHORN: That's exactly right. 483 00:26:01,980 --> 00:26:06,010 The value of pointer y is this address right here, and we're storing that in 484 00:26:06,010 --> 00:26:07,670 the variable pointer x. 485 00:26:07,670 --> 00:26:12,740 So this becomes 222. 486 00:26:12,740 --> 00:26:16,010 And I'm going to move the arrow, too, to signify that it now 487 00:26:16,010 --> 00:26:17,260 has a different address. 488 00:26:17,260 --> 00:26:24,560 489 00:26:24,560 --> 00:26:27,970 >> So now pointer underscore x and pointer underscore y 490 00:26:27,970 --> 00:26:29,250 have the same value. 491 00:26:29,250 --> 00:26:30,390 That's what that line does. 492 00:26:30,390 --> 00:26:34,170 So essentially, they're both pointing to y. 493 00:26:34,170 --> 00:26:37,180 If we just had our pointers, and we didn't really know the variables, we 494 00:26:37,180 --> 00:26:38,210 would have lost x. 495 00:26:38,210 --> 00:26:42,230 But thankfully, we have x saved in this function, so we still 496 00:26:42,230 --> 00:26:43,350 know where x is. 497 00:26:43,350 --> 00:26:48,660 But we'll see that problem coming up later on, that if we lose a pointer to 498 00:26:48,660 --> 00:26:51,790 something, we might not actually know where it is or be able to recover it. 499 00:26:51,790 --> 00:26:53,540 But that's for the future. 500 00:26:53,540 --> 00:26:55,310 So for right now, that's exactly right, Annie. 501 00:26:55,310 --> 00:26:57,350 >> And finally, that last line of code. 502 00:26:57,350 --> 00:27:00,876 Can somebody give me what the last line of code does? 503 00:27:00,876 --> 00:27:01,742 Yes? 504 00:27:01,742 --> 00:27:09,245 >> STUDENT: It multiplies what is in pointer y, so 16 times 16-- 505 00:27:09,245 --> 00:27:10,810 >> JASON HIRSCHHORN: 16 is not in pointer y. 506 00:27:10,810 --> 00:27:14,530 >> STUDENT: Well, it goes to that address, finds it, and then multiplies 507 00:27:14,530 --> 00:27:17,370 that by the same thing-- it does the same thing with pointer z, multiplies 508 00:27:17,370 --> 00:27:19,280 the values, and stores it in x. 509 00:27:19,280 --> 00:27:19,960 >> JASON HIRSCHHORN: Exactly right. 510 00:27:19,960 --> 00:27:22,540 So what do I need to update? 511 00:27:22,540 --> 00:27:23,463 What should x be? 512 00:27:23,463 --> 00:27:25,520 >> STUDENT: The top left, 256. 513 00:27:25,520 --> 00:27:28,620 >> JASON HIRSCHHORN: And what do I change it to? 514 00:27:28,620 --> 00:27:31,160 Sorry, we'll go back up. 515 00:27:31,160 --> 00:27:33,605 >> STUDENT: It should still be the same. 516 00:27:33,605 --> 00:27:36,020 >> JASON HIRSCHHORN: Yes, it actually still equals 256. 517 00:27:36,020 --> 00:27:37,630 But that's exactly right. 518 00:27:37,630 --> 00:27:38,850 Let's break this down. 519 00:27:38,850 --> 00:27:43,470 We'll see right here, this thing in parentheses, star pointer y, that 520 00:27:43,470 --> 00:27:46,850 treats what's in pointer y as an address, goes to that 521 00:27:46,850 --> 00:27:48,670 address, takes the 16. 522 00:27:48,670 --> 00:27:51,570 So now we have a 16, and we're multiplying that by the 523 00:27:51,570 --> 00:27:52,710 same thing in z. 524 00:27:52,710 --> 00:27:54,895 Going to that address, taking a 16. 525 00:27:54,895 --> 00:27:59,820 16 times 16 is 256, and we're storing that inside of x. x already happens to 526 00:27:59,820 --> 00:28:02,000 equal 256, so we're not going to make change. 527 00:28:02,000 --> 00:28:04,590 528 00:28:04,590 --> 00:28:08,550 >> You don't have this file right now, but in the source code for this 529 00:28:08,550 --> 00:28:12,880 section which will be posted online at cs50.net/sections, I have written all 530 00:28:12,880 --> 00:28:14,780 of this up, exactly what we did. 531 00:28:14,780 --> 00:28:17,780 And I actually included some other information right here, which we're 532 00:28:17,780 --> 00:28:19,610 going to go through in a second. 533 00:28:19,610 --> 00:28:24,040 >> We do these operations , as you can see right here, and then print out the 534 00:28:24,040 --> 00:28:27,860 resulting value of the specific variable that has been changed. 535 00:28:27,860 --> 00:28:28,880 This code's already been written. 536 00:28:28,880 --> 00:28:30,130 I'm going to run this code. 537 00:28:30,130 --> 00:28:36,110 538 00:28:36,110 --> 00:28:39,170 >> And now let's walk through the output. 539 00:28:39,170 --> 00:28:40,860 Let's start at the beginning. 540 00:28:40,860 --> 00:28:43,200 Value of x equals is 2. 541 00:28:43,200 --> 00:28:44,120 That should make sense. 542 00:28:44,120 --> 00:28:46,600 That was what we initially initialized it to. 543 00:28:46,600 --> 00:28:51,650 >> Address of x is this crazy thing. 544 00:28:51,650 --> 00:28:55,510 Again, we see, however, even though it looks a little bit crazy upon first 545 00:28:55,510 --> 00:28:57,980 inspection, we see that 0x right there. 546 00:28:57,980 --> 00:29:00,040 This is a hexadecimal number. 547 00:29:00,040 --> 00:29:03,600 >> And then we see all of this crazy stuff. 548 00:29:03,600 --> 00:29:05,860 There's a b and an f, and also a c in there. 549 00:29:05,860 --> 00:29:09,460 None of those are normal zero through nine digits, but because hexadecimal 550 00:29:09,460 --> 00:29:14,310 is base 16, you actually need a couple of other symbols to 551 00:29:14,310 --> 00:29:16,940 represent each digit. 552 00:29:16,940 --> 00:29:19,860 So again, we're not going to talk about hexadecimal too much, but know 553 00:29:19,860 --> 00:29:23,990 that when you're writing hexadecimal numbers, you'll see anything from zero 554 00:29:23,990 --> 00:29:28,830 through 9 plus things from a through f. 555 00:29:28,830 --> 00:29:33,140 So even though they're not all zero through nine numbers, this is a 556 00:29:33,140 --> 00:29:34,760 number, this is an address. 557 00:29:34,760 --> 00:29:38,540 And it could be expressed in base 10 if we wanted to. 558 00:29:38,540 --> 00:29:46,500 >> The value of pointer x right here is also a hexadecimal number, and what is 559 00:29:46,500 --> 00:29:47,520 it the same of? 560 00:29:47,520 --> 00:29:49,510 What is this number the same as? 561 00:29:49,510 --> 00:29:53,910 562 00:29:53,910 --> 00:29:55,290 >> Address of x, right? 563 00:29:55,290 --> 00:29:59,440 The line right above it, those two addresses are identical, and that's 564 00:29:59,440 --> 00:30:00,610 what we expect. 565 00:30:00,610 --> 00:30:06,830 The address of x is the value of pointer to x. 566 00:30:06,830 --> 00:30:10,020 >> Next, we have the address of pointer x, and that is, 567 00:30:10,020 --> 00:30:11,680 again, some random number. 568 00:30:11,680 --> 00:30:15,390 If we're going to inspect it, we notice that it shares a lot of the 569 00:30:15,390 --> 00:30:16,410 same numbers as the other ones. 570 00:30:16,410 --> 00:30:20,200 It's different, but it shares a lot of the same numbers. 571 00:30:20,200 --> 00:30:22,700 Recollect talking about buffer overflow errors? 572 00:30:22,700 --> 00:30:24,900 When you can write over things that are close to you in memory, if you go 573 00:30:24,900 --> 00:30:26,340 beyond the bounds of an array. 574 00:30:26,340 --> 00:30:28,920 Again, we're not going to talk about that right now, but for those of you 575 00:30:28,920 --> 00:30:31,510 curious, these things are actually pretty close to one another. 576 00:30:31,510 --> 00:30:34,400 These boxes are pretty close to one another in memory. 577 00:30:34,400 --> 00:30:38,660 >> And finally, pointer x points to the value, and of course, it's 2. 578 00:30:38,660 --> 00:30:39,920 I did that star. 579 00:30:39,920 --> 00:30:43,310 We followed the address in pointer x initially, and it 580 00:30:43,310 --> 00:30:45,340 returned a value of x. 581 00:30:45,340 --> 00:30:47,870 >> Finally, we see exactly what we just did. 582 00:30:47,870 --> 00:30:52,120 First z is 16, then x is 16, then y is 16. 583 00:30:52,120 --> 00:30:55,000 x eventually becomes 256 right here. 584 00:30:55,000 --> 00:30:58,090 And again, x is not changed by that last line of code. 585 00:30:58,090 --> 00:31:00,950 It remains a 256. 586 00:31:00,950 --> 00:31:03,380 >> If you want to repeat this on your own once again, or go through it, or 587 00:31:03,380 --> 00:31:05,690 change some of these values up, I've already written this code. 588 00:31:05,690 --> 00:31:11,490 You can make the changes to the code and see how those changes actually 589 00:31:11,490 --> 00:31:14,540 play out in real life. 590 00:31:14,540 --> 00:31:16,855 >> Before we move on with pointers, does anybody have any questions? 591 00:31:16,855 --> 00:31:20,924 592 00:31:20,924 --> 00:31:21,390 >> OK. 593 00:31:21,390 --> 00:31:26,620 Can everybody take one hand, and put it up in the air, and-- 594 00:31:26,620 --> 00:31:28,090 close your eyes-- 595 00:31:28,090 --> 00:31:32,700 put your right hand up in the air as a fist, and give me a thumbs up, thumbs 596 00:31:32,700 --> 00:31:36,590 down, thumbs middle, how comfortable you are with pointers so far. 597 00:31:36,590 --> 00:31:37,840 Close your eyes. 598 00:31:37,840 --> 00:31:40,770 599 00:31:40,770 --> 00:31:41,050 >> OK. 600 00:31:41,050 --> 00:31:43,360 You can put your hands down and open your eyes. 601 00:31:43,360 --> 00:31:44,610 Thank you. 602 00:31:44,610 --> 00:31:47,110 603 00:31:47,110 --> 00:31:47,750 >> We're going to move on. 604 00:31:47,750 --> 00:31:49,395 We're going to keep talking about pointers, but we're going to talk 605 00:31:49,395 --> 00:31:51,590 about them in a little bit of a different context. 606 00:31:51,590 --> 00:31:54,160 We're going to talk about arrays and pointers. 607 00:31:54,160 --> 00:31:56,660 And remember last week I blew your minds? 608 00:31:56,660 --> 00:31:58,480 Somebody's brains, they left them here on the floor. 609 00:31:58,480 --> 00:32:01,335 I was pretty sure blew that person's mind. 610 00:32:01,335 --> 00:32:02,925 That was a bad joke. 611 00:32:02,925 --> 00:32:03,220 >> [LAUGHTER] 612 00:32:03,220 --> 00:32:03,990 >> JASON HIRSCHHORN: I'm going to work on that. 613 00:32:03,990 --> 00:32:06,535 But anyway, strings we found out were arrays. 614 00:32:06,535 --> 00:32:08,750 Arrays are actually pointers. 615 00:32:08,750 --> 00:32:09,920 >> What do I mean by that? 616 00:32:09,920 --> 00:32:14,710 Well, if we create an array of six characters, which this line of code 617 00:32:14,710 --> 00:32:16,890 does right here-- 618 00:32:16,890 --> 00:32:21,380 char, array, open bracket, six, close bracket, semicolon-- again, creates an 619 00:32:21,380 --> 00:32:23,490 array of six characters. 620 00:32:23,490 --> 00:32:24,070 Fill it. 621 00:32:24,070 --> 00:32:25,570 >> I have a picture of it right here. 622 00:32:25,570 --> 00:32:28,900 These boxes, we see that filled it with some characters and the null 623 00:32:28,900 --> 00:32:29,900 terminator at the end. 624 00:32:29,900 --> 00:32:32,630 And down below, this is where I'd like you to direct your attention. 625 00:32:32,630 --> 00:32:34,230 The very first box-- 626 00:32:34,230 --> 00:32:35,200 we've done this before. 627 00:32:35,200 --> 00:32:37,230 We've actually done this in every problem set. 628 00:32:37,230 --> 00:32:42,610 I can refer to the very first box in this array as array bracket zero. 629 00:32:42,610 --> 00:32:44,970 We've done that a million times. 630 00:32:44,970 --> 00:32:47,860 >> I can actually refer to the same box another way. 631 00:32:47,860 --> 00:32:50,630 I can refer to it as star array. 632 00:32:50,630 --> 00:32:55,620 Because array, if we were actually to print out what array equals, array is 633 00:32:55,620 --> 00:33:01,340 an address, and it's an address of the first box in this array. 634 00:33:01,340 --> 00:33:06,590 So that star operator treats what's stored in array as an address, goes to 635 00:33:06,590 --> 00:33:09,180 that address, and returns the value there. 636 00:33:09,180 --> 00:33:15,870 Star array will return f just like array bracket zero returns f. 637 00:33:15,870 --> 00:33:19,850 >> Again, everywhere we've been writing array bracket zero, star array would 638 00:33:19,850 --> 00:33:21,440 have done the exact same thing. 639 00:33:21,440 --> 00:33:25,570 In fact, array bracket zero is just our sugar coating of 640 00:33:25,570 --> 00:33:26,270 what's actually happened. 641 00:33:26,270 --> 00:33:29,420 It's really star array is the more correct way, if there is one correct 642 00:33:29,420 --> 00:33:32,840 way, to think of what's actually happening. 643 00:33:32,840 --> 00:33:37,280 >> So that's all well and good, but here's where it gets fantastic. 644 00:33:37,280 --> 00:33:40,560 We go to the second character here, this zero. 645 00:33:40,560 --> 00:33:42,870 Should be an O, but we'll give it a zero. 646 00:33:42,870 --> 00:33:45,570 We're referred to that before, the second box in an array, as array 647 00:33:45,570 --> 00:33:49,000 bracket one whenever we've iterated through an array. 648 00:33:49,000 --> 00:33:51,860 >> There's another way to refer to it, and that's down below. 649 00:33:51,860 --> 00:33:57,750 That star, open paren, array plus 1, closed paren. 650 00:33:57,750 --> 00:34:01,400 So because arrays are laid out-- these boxes are all right next to each other 651 00:34:01,400 --> 00:34:03,350 in memory-- 652 00:34:03,350 --> 00:34:06,110 star array goes to the first one. 653 00:34:06,110 --> 00:34:10,940 So if we're going to take array that address, add 1 to it, and then go to 654 00:34:10,940 --> 00:34:13,070 that new address, that's going to be the thing right next to 655 00:34:13,070 --> 00:34:14,150 the original address. 656 00:34:14,150 --> 00:34:14,896 >> Yeah? 657 00:34:14,896 --> 00:34:18,810 >> STUDENT: So if array was, instead of being chars, it would be ints, then 658 00:34:18,810 --> 00:34:21,659 you'd have to increment by 4 every time? 659 00:34:21,659 --> 00:34:22,980 >> JASON HIRSCHHORN: that's a great question. 660 00:34:22,980 --> 00:34:24,850 Let me draw something first, then I'm going to answer your question. 661 00:34:24,850 --> 00:34:47,030 662 00:34:47,030 --> 00:34:49,300 >> To draw this up on the screen really quickly before getting to your 663 00:34:49,300 --> 00:34:51,219 question, we have our array-- 664 00:34:51,219 --> 00:34:57,690 665 00:34:57,690 --> 00:35:00,530 and it goes on, but I'm only going to focus on the first couple of ones. 666 00:35:00,530 --> 00:35:04,800 667 00:35:04,800 --> 00:35:08,020 In memory, there's a box that we created when we 668 00:35:08,020 --> 00:35:10,130 initialized this array. 669 00:35:10,130 --> 00:35:12,795 This is the array variable, and it is an address. 670 00:35:12,795 --> 00:35:17,420 It is, in fact, the address of the first thing, the first box. 671 00:35:17,420 --> 00:35:18,740 So this box is somewhere. 672 00:35:18,740 --> 00:35:21,760 These six boxes are somewhere else. 673 00:35:21,760 --> 00:35:24,810 >> When we do array bracket zero, we're going to get the first thing here. 674 00:35:24,810 --> 00:35:28,370 And when we do star array, we're going to get the exact same thing, the value 675 00:35:28,370 --> 00:35:30,320 that array is pointing to. 676 00:35:30,320 --> 00:35:35,360 >> When we do array bracket 1, we're going to get the second box, but also, 677 00:35:35,360 --> 00:35:39,630 because the pointer is outside those parentheses, first we're going to add 678 00:35:39,630 --> 00:35:43,520 1 to this address, and then we're going to follow that and give us the 679 00:35:43,520 --> 00:35:45,220 second box. 680 00:35:45,220 --> 00:35:47,800 And so on and so forth. 681 00:35:47,800 --> 00:35:51,860 >> Array plus 2 is going to add 2 to this address here, because again, addresses 682 00:35:51,860 --> 00:35:54,640 are just numbers represented as hexadecimal, often. 683 00:35:54,640 --> 00:36:00,070 So add 2 to it, and follow that to the third box, which happens to be, as you 684 00:36:00,070 --> 00:36:04,710 can see, two spaces away from the first box. 685 00:36:04,710 --> 00:36:07,470 >> Hopefully, that made it less confusing rather than more confusing. 686 00:36:07,470 --> 00:36:08,540 But did that make it more confusing? 687 00:36:08,540 --> 00:36:10,030 Again, I'm going to get to your question in one second. 688 00:36:10,030 --> 00:36:12,250 Did that make it more confusing for anybody, or does anybody have any 689 00:36:12,250 --> 00:36:14,820 questions about this idea of arrays as pointers? 690 00:36:14,820 --> 00:36:20,080 691 00:36:20,080 --> 00:36:21,560 >> Fantastic. 692 00:36:21,560 --> 00:36:23,450 Also it'd be fantastic if you had questions. 693 00:36:23,450 --> 00:36:27,780 But moving on to your question. 694 00:36:27,780 --> 00:36:30,910 695 00:36:30,910 --> 00:36:37,340 >> The question was integers in memory take up four bytes, so four boxes. 696 00:36:37,340 --> 00:36:42,050 If we think of one boxes as one byte, integers take up four boxes. 697 00:36:42,050 --> 00:36:44,560 >> So say these are integers now. 698 00:36:44,560 --> 00:36:50,680 This is an int array right here. 699 00:36:50,680 --> 00:36:52,570 I'm going to make up some random addresses. 700 00:36:52,570 --> 00:36:58,410 So if this was an int array, the first one might be a 10. 701 00:36:58,410 --> 00:37:03,210 The next one's four boxes over because integers are four boxes big. 702 00:37:03,210 --> 00:37:07,690 There's be 10, 11, 12, 13, so this one would be a 14. 703 00:37:07,690 --> 00:37:08,870 This one would be an 18. 704 00:37:08,870 --> 00:37:13,830 This one would be a 22, and so on. 705 00:37:13,830 --> 00:37:15,590 >> So again, chars are one byte. 706 00:37:15,590 --> 00:37:17,380 They're right next to each other, so this map we were talking 707 00:37:17,380 --> 00:37:19,190 before works perfectly. 708 00:37:19,190 --> 00:37:20,710 >> But you just brought up the case of ints. 709 00:37:20,710 --> 00:37:23,440 Well, if I added 1 to this, it would go to 11. 710 00:37:23,440 --> 00:37:25,710 It'd be in the middle of this int if I followed it really 711 00:37:25,710 --> 00:37:26,710 specifically like that. 712 00:37:26,710 --> 00:37:30,070 I was actually going box by box in memory. 713 00:37:30,070 --> 00:37:36,750 >> The cool thing about this type of pointer arithmetic is the program is 714 00:37:36,750 --> 00:37:38,840 smart enough to know what you mean. 715 00:37:38,840 --> 00:37:46,000 When you're adding 1 to a memory address that is pointing to an 716 00:37:46,000 --> 00:37:49,470 integer, it's not actually going to add one block over. 717 00:37:49,470 --> 00:37:51,150 It's going to add four blocks over. 718 00:37:51,150 --> 00:37:54,530 It's going to scale that number for you buy the size. 719 00:37:54,530 --> 00:37:57,780 >> And this goes back to Curtis's question, why do we do int star rather 720 00:37:57,780 --> 00:37:58,595 than char star? 721 00:37:58,595 --> 00:38:01,050 Was that your question? 722 00:38:01,050 --> 00:38:02,220 Somebody asked that earlier, right? 723 00:38:02,220 --> 00:38:06,200 Why we do int star rather than char star if it's going to address no 724 00:38:06,200 --> 00:38:06,960 matter what? 725 00:38:06,960 --> 00:38:09,790 And that's to help us with this type of pointer arithmetic. 726 00:38:09,790 --> 00:38:12,660 If we declare something as a char star but it's really going through 727 00:38:12,660 --> 00:38:16,890 integers, when we added 1, it would only move one box over rather than the 728 00:38:16,890 --> 00:38:21,540 four we logically expect it to move over. 729 00:38:21,540 --> 00:38:26,180 >> So again, in pointer arithmetic, when we're treating these things like 730 00:38:26,180 --> 00:38:29,890 addresses, then adding or subtracting to them and then following them to 731 00:38:29,890 --> 00:38:35,410 figure out what's going on in memory, the number we add to them gets scaled 732 00:38:35,410 --> 00:38:39,491 by the size of the type of thing they're pointing to. 733 00:38:39,491 --> 00:38:41,100 >> So if they're pointing to an integer-- 734 00:38:41,100 --> 00:38:42,380 I know I'm repeating myself-- 735 00:38:42,380 --> 00:38:45,030 but if they're an integer, you're actually going to move four 736 00:38:45,030 --> 00:38:46,760 boxes over each time. 737 00:38:46,760 --> 00:38:49,230 If they're a char, you're going to move one box. 738 00:38:49,230 --> 00:38:52,720 If they are a long long, which is eight bytes, you're going to move 739 00:38:52,720 --> 00:38:53,880 eight boxes over. 740 00:38:53,880 --> 00:38:55,940 Whatever the size of the data type, who knows? 741 00:38:55,940 --> 00:38:57,690 Doesn't really matter for our purposes. 742 00:38:57,690 --> 00:39:01,450 All that we need to know is actually, this math right here, just adding 1 743 00:39:01,450 --> 00:39:04,280 will get us to the second box regardless of what type of thing is in 744 00:39:04,280 --> 00:39:05,530 that second box. 745 00:39:05,530 --> 00:39:09,860 746 00:39:09,860 --> 00:39:13,870 >> I hope there's some type of questions here, because that was a lot, and I 747 00:39:13,870 --> 00:39:15,780 could not have done a perfect job at explaining that. 748 00:39:15,780 --> 00:39:18,280 749 00:39:18,280 --> 00:39:19,260 Really? 750 00:39:19,260 --> 00:39:20,980 No questions? 751 00:39:20,980 --> 00:39:23,660 For the audience at home-- there we go. 752 00:39:23,660 --> 00:39:27,490 >> STUDENT: If you were to make an array in a function, and you want it to 753 00:39:27,490 --> 00:39:31,095 return that array to something else, how would you return it? 754 00:39:31,095 --> 00:39:32,830 >> JASON HIRSCHHORN: Excellent question. 755 00:39:32,830 --> 00:39:37,260 So if you wanted to make an array in a function, and then return that array-- 756 00:39:37,260 --> 00:39:41,300 so we'll say we'll make an array in a function called by main. 757 00:39:41,300 --> 00:39:45,750 Main calls this function, and this function creates the array, then 758 00:39:45,750 --> 00:39:47,100 returns it to main. 759 00:39:47,100 --> 00:39:49,370 >> Now we've seen something similar to this before. 760 00:39:49,370 --> 00:39:52,810 Where have we seen something similar where main creates the array, and then 761 00:39:52,810 --> 00:39:55,880 the function manipulates it? 762 00:39:55,880 --> 00:39:58,380 What have we written before that's done that? 763 00:39:58,380 --> 00:39:59,626 >> STUDENT: Sorting? 764 00:39:59,626 --> 00:40:01,890 >> JASON HIRSCHHORN: Sorting has done that. 765 00:40:01,890 --> 00:40:03,980 Game of 15 has also done that. 766 00:40:03,980 --> 00:40:09,680 In sorting, we passed in the array, the haystack, and then we sorted it by 767 00:40:09,680 --> 00:40:13,100 destructively updating different parts in that array. 768 00:40:13,100 --> 00:40:16,380 So we took our original array-- remember, one of the parameters to the 769 00:40:16,380 --> 00:40:18,010 sorting function was array-- 770 00:40:18,010 --> 00:40:19,858 >> STUDENT: Isn't that a global array? 771 00:40:19,858 --> 00:40:20,420 >> JASON HIRSCHHORN: Right. 772 00:40:20,420 --> 00:40:21,610 Yeah, we're going to get back there. 773 00:40:21,610 --> 00:40:23,970 But had it been created in-- 774 00:40:23,970 --> 00:40:26,510 was it a global, or was it created in the function? 775 00:40:26,510 --> 00:40:28,115 Let's look. 776 00:40:28,115 --> 00:40:29,638 We can answer that. 777 00:40:29,638 --> 00:40:49,700 778 00:40:49,700 --> 00:40:51,750 >> It looks like it's not a global array. 779 00:40:51,750 --> 00:40:56,620 It has created right here on this line an array of integers, and then it's 780 00:40:56,620 --> 00:40:59,820 passed to our sorting function. 781 00:40:59,820 --> 00:41:00,660 So it isn't a global array. 782 00:41:00,660 --> 00:41:02,810 It's created in main, passed to our sorting function. 783 00:41:02,810 --> 00:41:05,690 Our sorting function destructively updates it, and returns it. 784 00:41:05,690 --> 00:41:09,310 As we can see up at top, sort-- 785 00:41:09,310 --> 00:41:10,410 oh, my goodness, sorry. 786 00:41:10,410 --> 00:41:12,790 >> STUDENT: Sort just returns whether or not it found it? 787 00:41:12,790 --> 00:41:13,350 >> JASON HIRSCHHORN: Right. 788 00:41:13,350 --> 00:41:14,120 Sorry. 789 00:41:14,120 --> 00:41:16,320 Sort actually-- let's open that up. 790 00:41:16,320 --> 00:41:20,320 791 00:41:20,320 --> 00:41:21,310 Sort is void. 792 00:41:21,310 --> 00:41:24,350 Sort doesn't return anything because sort destructively updates the array, 793 00:41:24,350 --> 00:41:26,340 the original array. 794 00:41:26,340 --> 00:41:30,520 >> So that's what we've seen before, but your question was what if sort 795 00:41:30,520 --> 00:41:32,155 actually creates the array? 796 00:41:32,155 --> 00:41:36,870 >> STUDENT: Refer returns the updated array, or something like that. 797 00:41:36,870 --> 00:41:39,030 >> JASON HIRSCHHORN: Another thing that could have been done was main called 798 00:41:39,030 --> 00:41:39,840 the function. 799 00:41:39,840 --> 00:41:44,570 That function's job is to create an array of something, an array of 800 00:41:44,570 --> 00:41:49,390 integers, and then return that array it's created to main. 801 00:41:49,390 --> 00:41:55,080 >> So as we've seen before when talking about the stack, which we're going to 802 00:41:55,080 --> 00:42:00,390 talk about a bit later today, once a function returns, everything that was 803 00:42:00,390 --> 00:42:03,490 created in that function, all of its local variables, go away. 804 00:42:03,490 --> 00:42:04,910 We've seen that idea of scope before. 805 00:42:04,910 --> 00:42:07,750 That's why we've created global variables sometime, to escape this 806 00:42:07,750 --> 00:42:11,790 problem of things going away when their functions return. 807 00:42:11,790 --> 00:42:15,970 >> What you would do in that case is the function would create the array not as 808 00:42:15,970 --> 00:42:17,870 a local variable on the stack. 809 00:42:17,870 --> 00:42:21,670 It would create it in a different part of memory that allows things to be 810 00:42:21,670 --> 00:42:26,050 saved even when functions have returned and the function frame, the 811 00:42:26,050 --> 00:42:28,330 stack frame, has left the stack. 812 00:42:28,330 --> 00:42:29,770 >> I'm going to use some terminology here. 813 00:42:29,770 --> 00:42:31,620 You're not going to hear about this until tomorrow, but that area of 814 00:42:31,620 --> 00:42:34,320 memory is called the heap, and I don't want to go into that. 815 00:42:34,320 --> 00:42:39,140 But what that function could do, again to oversimplify potentially, is create 816 00:42:39,140 --> 00:42:43,390 that array in a different space in memory that actually stays there for 817 00:42:43,390 --> 00:42:47,920 as long as you want it to stay there, and it'll pass back to main a pointer 818 00:42:47,920 --> 00:42:51,400 to that array saved in that other part of memory. 819 00:42:51,400 --> 00:42:54,960 So it makes some boxes somewhere else that don't get destroyed when the 820 00:42:54,960 --> 00:43:00,770 function returns and passes back to main an address to that set of boxes. 821 00:43:00,770 --> 00:43:03,760 >> That's probably not a sufficient enough answer to your question, but 822 00:43:03,760 --> 00:43:05,520 that will be explored more tomorrow when you guys talk 823 00:43:05,520 --> 00:43:06,770 about the heap in lecture. 824 00:43:06,770 --> 00:43:09,330 825 00:43:09,330 --> 00:43:11,590 >> Let us move on-- oh, my goodness! 826 00:43:11,590 --> 00:43:12,850 That's a dolphin! 827 00:43:12,850 --> 00:43:14,650 How adorable is that? 828 00:43:14,650 --> 00:43:15,660 God! 829 00:43:15,660 --> 00:43:18,170 They're my favorite animal, so they are indeed adorable. 830 00:43:18,170 --> 00:43:19,720 I have three dolphin stuffed animals. 831 00:43:19,720 --> 00:43:21,930 But enough about me. 832 00:43:21,930 --> 00:43:23,910 >> You all have your computers open in front of you. 833 00:43:23,910 --> 00:43:25,530 Here is your coding assignment. 834 00:43:25,530 --> 00:43:28,650 We're going to start with pseudocode, though, so don't get too excited. 835 00:43:28,650 --> 00:43:32,780 >> You need to write a program that takes one and only one command line 836 00:43:32,780 --> 00:43:35,990 argument, and that's the number of dolphins to be trained. 837 00:43:35,990 --> 00:43:38,470 That number should be greater than 0. 838 00:43:38,470 --> 00:43:41,160 >> Next you're going to allow the trainer to enter an age for 839 00:43:41,160 --> 00:43:42,540 each of these dolphins. 840 00:43:42,540 --> 00:43:45,540 Each age, again, should be greater than 0. 841 00:43:45,540 --> 00:43:49,930 And all of these ages should be stored in an array. 842 00:43:49,930 --> 00:43:53,740 >> To get an age, however, you need to call a function with the following 843 00:43:53,740 --> 00:43:55,580 prototype-- 844 00:43:55,580 --> 00:44:01,750 int star, space, getAge, parentheses, void, close parentheses, semicolon. 845 00:44:01,750 --> 00:44:06,380 >> Finally, I want you to print out the age of the oldest dolphin. 846 00:44:06,380 --> 00:44:10,490 So again, before you start writing in code, have a sheet of paper or have 847 00:44:10,490 --> 00:44:14,020 gedit open if you want, and write out some pseudocode for this. 848 00:44:14,020 --> 00:44:16,560 In three minutes, we're going to go over the pseudocode. 849 00:44:16,560 --> 00:44:19,260 You'll have another three minutes to code it, and then we'll go over coding 850 00:44:19,260 --> 00:44:21,180 it together. 851 00:44:21,180 --> 00:44:25,370 >> And if you have any questions because this isn't clear, raise your hand and 852 00:44:25,370 --> 00:44:27,720 I'll come around, and I'm happy to answer your question. 853 00:44:27,720 --> 00:44:31,080 But to start, take three minutes to write pseudocode to make sure you 854 00:44:31,080 --> 00:44:32,675 understand the logic of this problem. 855 00:44:32,675 --> 00:46:40,160 856 00:46:40,160 --> 00:46:44,030 >> Let's take a stab at the pseudocode as a group. 857 00:46:44,030 --> 00:46:46,780 I'm going to write this on the board as you give me the pseudocode. 858 00:46:46,780 --> 00:46:49,350 Who would like to give me the first line? 859 00:46:49,350 --> 00:46:51,170 First line's maybe the easiest, so if you haven't 860 00:46:51,170 --> 00:46:54,010 participated yet, start here. 861 00:46:54,010 --> 00:46:56,020 What's the first line of pseudocode. 862 00:46:56,020 --> 00:47:02,322 >> STUDENT: First line is to check for the number of arguments that you get. 863 00:47:02,322 --> 00:47:03,768 >> JASON HIRSCHHORN: Exactly right. 864 00:47:03,768 --> 00:47:10,516 865 00:47:10,516 --> 00:47:14,150 Check number of arguments, first thing we need to do. 866 00:47:14,150 --> 00:47:15,400 Next? 867 00:47:15,400 --> 00:47:22,010 868 00:47:22,010 --> 00:47:27,776 >> STUDENT: Check that that command, the first argument, is greater than 0? 869 00:47:27,776 --> 00:47:31,050 >> JASON HIRSCHHORN: Check that the first argument is greater than 0, yes. 870 00:47:31,050 --> 00:47:38,930 871 00:47:38,930 --> 00:47:40,400 Next line, go ahead. 872 00:47:40,400 --> 00:47:47,020 >> STUDENT: Then you'd probably want to store that command line argument into 873 00:47:47,020 --> 00:47:51,380 a variable, or you could just go to the next step. 874 00:47:51,380 --> 00:47:55,340 >> JASON HIRSCHHORN: Yes, again, we'll have a string here, but we want an 875 00:47:55,340 --> 00:47:57,600 integer because it's a number, so we need to make the 876 00:47:57,600 --> 00:47:58,650 string into an integer. 877 00:47:58,650 --> 00:47:59,210 >> STUDENT: x, y. 878 00:47:59,210 --> 00:48:00,090 >> JASON HIRSCHHORN: That's right. 879 00:48:00,090 --> 00:48:01,970 So, we'll write that. 880 00:48:01,970 --> 00:48:04,542 String-- whoa, look at that marker. 881 00:48:04,542 --> 00:48:06,760 That's the second thing we need to do, take the string and 882 00:48:06,760 --> 00:48:07,490 turn it into an int. 883 00:48:07,490 --> 00:48:08,740 What's next? 884 00:48:08,740 --> 00:48:13,220 885 00:48:13,220 --> 00:48:18,770 >> STUDENT: Create an array of ints with the size of the first command line 886 00:48:18,770 --> 00:48:20,190 argument that's not programmed? 887 00:48:20,190 --> 00:48:20,950 >> JASON HIRSCHHORN: Sorry? 888 00:48:20,950 --> 00:48:26,957 >> STUDENT: Just create an array of ints, and make it the size of the command 889 00:48:26,957 --> 00:48:28,260 line argument that you got. 890 00:48:28,260 --> 00:48:30,920 >> JASON HIRSCHHORN: Create array of size-- 891 00:48:30,920 --> 00:48:33,930 we'll do arg, whatever that argument was. 892 00:48:33,930 --> 00:48:34,850 OK? 893 00:48:34,850 --> 00:48:36,100 What's next? 894 00:48:36,100 --> 00:48:44,310 895 00:48:44,310 --> 00:48:48,230 >> So we have the array, allow the trainer to enter an 896 00:48:48,230 --> 00:48:50,096 age for each dolphin. 897 00:48:50,096 --> 00:48:52,790 To get an age, call a function with the following prototype. 898 00:48:52,790 --> 00:48:53,480 So we have our array. 899 00:48:53,480 --> 00:48:54,350 What do we need to do? 900 00:48:54,350 --> 00:48:56,805 >> STUDENT: For n dolphins? 901 00:48:56,805 --> 00:48:58,110 >> JASON HIRSCHHORN: OK. 902 00:48:58,110 --> 00:48:59,640 I'm going to write for each. 903 00:48:59,640 --> 00:49:01,580 For each dolphin, what do we need to do? 904 00:49:01,580 --> 00:49:04,340 905 00:49:04,340 --> 00:49:05,785 Go ahead? 906 00:49:05,785 --> 00:49:08,400 >> STUDENT: Call the function. 907 00:49:08,400 --> 00:49:11,678 >> JASON HIRSCHHORN: Call the function, OK. 908 00:49:11,678 --> 00:49:15,640 >> STUDENT: In pseudocoding, should you define functions like in a separate 909 00:49:15,640 --> 00:49:16,660 block of pseudocode? 910 00:49:16,660 --> 00:49:17,650 >> JASON HIRSCHHORN: So, yeah. 911 00:49:17,650 --> 00:49:20,540 Let's do a separate box of pseudocode over here, pseudocode for getAge. 912 00:49:20,540 --> 00:49:23,600 So this function, for each dolphin we call getAge, and 913 00:49:23,600 --> 00:49:25,370 getAge returns something. 914 00:49:25,370 --> 00:49:27,840 So where should we put the thing we're returning? 915 00:49:27,840 --> 00:49:30,730 916 00:49:30,730 --> 00:49:32,690 It returns an int pointer, right? 917 00:49:32,690 --> 00:49:35,810 That's the return type, according to that function prototype. 918 00:49:35,810 --> 00:49:37,620 So where are we putting that int pointer. 919 00:49:37,620 --> 00:49:41,828 920 00:49:41,828 --> 00:49:43,040 >> STUDENT: Into the array. 921 00:49:43,040 --> 00:49:43,570 >> JASON HIRSCHHORN: Into the array. 922 00:49:43,570 --> 00:49:44,460 That sounds great. 923 00:49:44,460 --> 00:49:53,270 Call getAge, put return value in array. 924 00:49:53,270 --> 00:49:57,490 925 00:49:57,490 --> 00:49:58,850 >> Let's pause getAge for a second. 926 00:49:58,850 --> 00:49:59,855 We'll come back to that. 927 00:49:59,855 --> 00:50:01,310 And I'll let you start me on that, Marcus. 928 00:50:01,310 --> 00:50:02,790 But we've gotten each age. 929 00:50:02,790 --> 00:50:03,390 We've put them all in the array. 930 00:50:03,390 --> 00:50:04,640 What do we do at the end? 931 00:50:04,640 --> 00:50:07,170 932 00:50:07,170 --> 00:50:10,640 How do we finish main? 933 00:50:10,640 --> 00:50:13,630 What's the last thing we need to do? 934 00:50:13,630 --> 00:50:14,210 Alden? 935 00:50:14,210 --> 00:50:17,490 >> STUDENT: We could sort them, and then print the largest find. 936 00:50:17,490 --> 00:50:20,700 >> JASON HIRSCHHORN: We could sort them, yes. 937 00:50:20,700 --> 00:50:21,950 So how do we sort? 938 00:50:21,950 --> 00:50:25,670 939 00:50:25,670 --> 00:50:28,070 >> STUDENT: We could just like-- 940 00:50:28,070 --> 00:50:30,600 >> JASON HIRSCHHORN: Do we need to sort here, though? 941 00:50:30,600 --> 00:50:32,810 >> STUDENT: I suppose not. 942 00:50:32,810 --> 00:50:33,760 >> JASON HIRSCHHORN: OK. 943 00:50:33,760 --> 00:50:34,920 You did this before. 944 00:50:34,920 --> 00:50:37,300 What was the first way you wrote search for? 945 00:50:37,300 --> 00:50:39,930 >> STUDENT: We could just check each one against each other one. 946 00:50:39,930 --> 00:50:40,640 >> JASON HIRSCHHORN: Let's do that. 947 00:50:40,640 --> 00:50:44,090 Let's search through each one and just keep track of the oldest age. 948 00:50:44,090 --> 00:50:47,850 949 00:50:47,850 --> 00:50:54,110 Search for oldest, and then what do we need to do with the oldest? 950 00:50:54,110 --> 00:50:55,330 >> STUDENT: Return it. 951 00:50:55,330 --> 00:50:56,280 Print it out, sorry. 952 00:50:56,280 --> 00:50:56,950 >> JASON HIRSCHHORN: There we go. 953 00:50:56,950 --> 00:50:58,630 Exactly right. 954 00:50:58,630 --> 00:51:02,400 Print out oldest. 955 00:51:02,400 --> 00:51:02,870 >> OK. 956 00:51:02,870 --> 00:51:03,220 Marcus? 957 00:51:03,220 --> 00:51:05,800 >> STUDENT: Keep track of the oldest as you add them? 958 00:51:05,800 --> 00:51:07,900 >> JASON HIRSCHHORN: OK, search for oldest. 959 00:51:07,900 --> 00:51:11,910 So we could keep track of the oldest as we add them up there. 960 00:51:11,910 --> 00:51:16,360 Call getAge, put return value in the array, and then see if this is the 961 00:51:16,360 --> 00:51:18,950 current oldest one, maybe save it in a different variable? 962 00:51:18,950 --> 00:51:20,400 That would work as well. 963 00:51:20,400 --> 00:51:24,150 That might actually save us a trip, but this is fantastic. 964 00:51:24,150 --> 00:51:26,120 I'm going to leave this up here for now. 965 00:51:26,120 --> 00:51:28,420 But if you want to in your implementation take it that step 966 00:51:28,420 --> 00:51:29,980 further, that would be great. 967 00:51:29,980 --> 00:51:30,520 >> Marcus? 968 00:51:30,520 --> 00:51:31,950 Lets do getAge over here. 969 00:51:31,950 --> 00:51:33,930 >> MARCUS: OK. 970 00:51:33,930 --> 00:51:36,000 Ask the user for an age. 971 00:51:36,000 --> 00:51:38,400 >> JASON HIRSCHHORN: Ask the user for an age. 972 00:51:38,400 --> 00:51:39,650 OK. 973 00:51:39,650 --> 00:51:44,285 974 00:51:44,285 --> 00:51:46,252 Can everybody see that? 975 00:51:46,252 --> 00:51:47,900 Over here? 976 00:51:47,900 --> 00:51:51,530 OK, ask user for an age. 977 00:51:51,530 --> 00:51:52,780 Keep going, you got this. 978 00:51:52,780 --> 00:51:55,250 979 00:51:55,250 --> 00:51:58,900 >> MARCUS: I guess I don't really get why it's better to return an int star than 980 00:51:58,900 --> 00:52:02,020 just an int, like then to just return the age to store it. 981 00:52:02,020 --> 00:52:04,200 >> JASON HIRSCHHORN: It's not necessarily better to return. 982 00:52:04,200 --> 00:52:07,335 It would work just as well returning in, but I want you to return int star. 983 00:52:07,335 --> 00:52:08,585 >> MARCUS: OK. 984 00:52:08,585 --> 00:52:11,120 985 00:52:11,120 --> 00:52:12,620 I don't really know what-- 986 00:52:12,620 --> 00:52:13,050 >> JASON HIRSCHHORN: OK. 987 00:52:13,050 --> 00:52:14,430 Does anybody have any thoughts? 988 00:52:14,430 --> 00:52:15,580 We ask user for an age. 989 00:52:15,580 --> 00:52:16,830 We're getting an integer. 990 00:52:16,830 --> 00:52:23,970 991 00:52:23,970 --> 00:52:24,580 >> OK. 992 00:52:24,580 --> 00:52:28,140 So why don't we pause here. 993 00:52:28,140 --> 00:52:31,350 We're going to go to our next step in the logic, map these things on to see, 994 00:52:31,350 --> 00:52:35,480 and then you guys will code that first part, and well code all of that 995 00:52:35,480 --> 00:52:37,380 together, but a particular getAge. 996 00:52:37,380 --> 00:52:39,395 And we'll figure out what's going on with that int star. 997 00:52:39,395 --> 00:52:41,970 998 00:52:41,970 --> 00:52:45,440 >> But before we do that, before we jump into C code, can somebody point out 999 00:52:45,440 --> 00:52:49,682 some things they see in our pseudocode that would map onto regular code? 1000 00:52:49,682 --> 00:52:53,130 1001 00:52:53,130 --> 00:52:53,850 In C? 1002 00:52:53,850 --> 00:52:57,580 So some things in pseudocode, some words you see, some indentation you 1003 00:52:57,580 --> 00:53:00,435 see, some things that give you some hints about how to code this in C? 1004 00:53:00,435 --> 00:53:01,360 >> STUDENT: For loop. 1005 00:53:01,360 --> 00:53:01,930 >> JASON HIRSCHHORN: Where? 1006 00:53:01,930 --> 00:53:04,356 >> STUDENT: For "for each "dolphin." 1007 00:53:04,356 --> 00:53:06,310 >> JASON HIRSCHHORN: OK. 1008 00:53:06,310 --> 00:53:06,910 There we go. 1009 00:53:06,910 --> 00:53:07,920 We've got a loop there. 1010 00:53:07,920 --> 00:53:09,420 What else? 1011 00:53:09,420 --> 00:53:09,920 Mondo? 1012 00:53:09,920 --> 00:53:12,410 >> STUDENT: The string to int thing would be a to i 1013 00:53:12,410 --> 00:53:14,270 >> JASON HIRSCHHORN: OK. 1014 00:53:14,270 --> 00:53:15,850 So we've seen that function before. 1015 00:53:15,850 --> 00:53:19,180 1016 00:53:19,180 --> 00:53:20,430 Other stuff we see? 1017 00:53:20,430 --> 00:53:26,456 1018 00:53:26,456 --> 00:53:27,992 >> STUDENT: Printf. 1019 00:53:27,992 --> 00:53:29,920 >> JASON HIRSCHHORN: Exactly. 1020 00:53:29,920 --> 00:53:35,470 Not obviously, but hopefully we have some familiarity now with printf. 1021 00:53:35,470 --> 00:53:38,340 What else? 1022 00:53:38,340 --> 00:53:38,660 Yeah? 1023 00:53:38,660 --> 00:53:41,230 >> STUDENT: Reading an array is just declaring an array? 1024 00:53:41,230 --> 00:53:42,350 >> JASON HIRSCHHORN: OK. 1025 00:53:42,350 --> 00:53:45,410 So, of size arg. 1026 00:53:45,410 --> 00:53:46,190 That's right. 1027 00:53:46,190 --> 00:53:49,140 I don't know what to write that as, but yes, declaring one. 1028 00:53:49,140 --> 00:53:50,490 Another word for creating it. 1029 00:53:50,490 --> 00:53:53,070 Any other things we see up here? 1030 00:53:53,070 --> 00:53:53,830 Structures we know? 1031 00:53:53,830 --> 00:53:56,340 What about that line "search for oldest?" What kind of 1032 00:53:56,340 --> 00:53:57,290 structure might that use? 1033 00:53:57,290 --> 00:54:00,370 Alden, you suggested it. 1034 00:54:00,370 --> 00:54:01,620 >> STUDENT: Just search. 1035 00:54:01,620 --> 00:54:04,812 1036 00:54:04,812 --> 00:54:06,850 so like just a loop. 1037 00:54:06,850 --> 00:54:07,920 >> JASON HIRSCHHORN: A loop. 1038 00:54:07,920 --> 00:54:10,420 So, they search. 1039 00:54:10,420 --> 00:54:12,510 You're probably talking about linear search right here, so it's probably 1040 00:54:12,510 --> 00:54:14,550 just using a standard loop going through everything, which is why 1041 00:54:14,550 --> 00:54:17,630 Curtis suggested we could put it in the previous loop. 1042 00:54:17,630 --> 00:54:19,810 >> Any other things we see? 1043 00:54:19,810 --> 00:54:22,260 Check number of args. 1044 00:54:22,260 --> 00:54:23,300 How do we check something? 1045 00:54:23,300 --> 00:54:24,782 >> STUDENT: If statements. 1046 00:54:24,782 --> 00:54:31,710 1047 00:54:31,710 --> 00:54:32,130 And 1048 00:54:32,130 --> 00:54:33,640 >> JASON HIRSCHHORN: That's an if condition. 1049 00:54:33,640 --> 00:54:37,040 1050 00:54:37,040 --> 00:54:39,460 >> So, why don't you get started coding this program. 1051 00:54:39,460 --> 00:54:40,570 You have the pseudocode up here. 1052 00:54:40,570 --> 00:54:42,310 Start writing and in C code. 1053 00:54:42,310 --> 00:54:47,050 Again, we're going to figure out what's going on with that int star and 1054 00:54:47,050 --> 00:54:50,910 how we can actually write this program so that it conforms to the 1055 00:54:50,910 --> 00:54:52,870 specification I've given you. 1056 00:54:52,870 --> 00:54:57,330 If you want to, however, start with just int get void. 1057 00:54:57,330 --> 00:54:58,620 It just returns an integer. 1058 00:54:58,620 --> 00:55:00,070 >> That helps you get going. 1059 00:55:00,070 --> 00:55:01,380 Just code that. 1060 00:55:01,380 --> 00:55:04,760 Code it as just the return type as an int, not as an int star. 1061 00:55:04,760 --> 00:55:07,340 But then we are going to turn it into int star again so we can conform to 1062 00:55:07,340 --> 00:55:08,040 the specification. 1063 00:55:08,040 --> 00:55:12,220 Also explore how one might actually get at what Jeff was 1064 00:55:12,220 --> 00:55:13,550 talking about earlier. 1065 00:55:13,550 --> 00:55:16,800 So take three to five minutes to code this. 1066 00:55:16,800 --> 01:01:16,860 1067 01:01:16,860 --> 01:01:19,590 >> We are going to go through this code together. 1068 01:01:19,590 --> 01:01:23,360 I apologize if there was a lack of clarity on some parts of this. 1069 01:01:23,360 --> 01:01:26,240 But what we're going to look at right now is I've already typed up all the 1070 01:01:26,240 --> 01:01:29,690 code for what it will look like if we're just returning an 1071 01:01:29,690 --> 01:01:30,690 integer with getAge. 1072 01:01:30,690 --> 01:01:33,390 And we'll go through that code, and then we'll figure out how we can 1073 01:01:33,390 --> 01:01:38,900 actually get it to align with what was asked of us to return an int star. 1074 01:01:38,900 --> 01:01:39,560 >> Here's the code. 1075 01:01:39,560 --> 01:01:42,870 And again, I apologize that we can't code this out as a group, but I want 1076 01:01:42,870 --> 01:01:44,410 to make sure we get through it. 1077 01:01:44,410 --> 01:01:47,050 >> For the top, we have our function prototype, getAge. 1078 01:01:47,050 --> 01:01:48,920 We've seen that before. 1079 01:01:48,920 --> 01:01:54,490 Return type first, then name, then parameters. 1080 01:01:54,490 --> 01:01:57,930 >> Up here, we're including three libraries. 1081 01:01:57,930 --> 01:02:00,560 Can somebody tell me what this line 12 does? 1082 01:02:00,560 --> 01:02:02,020 Sharp include standard live.h? 1083 01:02:02,020 --> 01:02:05,410 1084 01:02:05,410 --> 01:02:06,410 What does that line do? 1085 01:02:06,410 --> 01:02:10,120 If I were to ask you this on a quiz, and I asked you give me the answer in 1086 01:02:10,120 --> 01:02:14,060 two sentences, what would you say? 1087 01:02:14,060 --> 01:02:15,310 >> STUDENT: [INAUDIBLE] 1088 01:02:15,310 --> 01:02:19,290 1089 01:02:19,290 --> 01:02:22,160 allows you to use all the functions stored in that file. 1090 01:02:22,160 --> 01:02:23,310 >> JASON HIRSCHHORN: That's a good answer. 1091 01:02:23,310 --> 01:02:28,140 It's the header function that has the prototypes of a number of functions, 1092 01:02:28,140 --> 01:02:31,130 and if you're going to use those in your library, this is saying these 1093 01:02:31,130 --> 01:02:32,900 functions exist. 1094 01:02:32,900 --> 01:02:37,510 What else do I need to do if I'm including, say, the CS50 library? 1095 01:02:37,510 --> 01:02:39,660 What else do I need to do when I compile my program? 1096 01:02:39,660 --> 01:02:43,550 >> STUDENT: [INAUDIBLE] include it using the dash l. 1097 01:02:43,550 --> 01:02:46,980 So we need to link in those CS50 library with dash l standing for link. 1098 01:02:46,980 --> 01:02:51,310 dash l CS50 links in the actual ones and zeros, the actual implementation 1099 01:02:51,310 --> 01:02:55,030 of, say, get string or get int. 1100 01:02:55,030 --> 01:02:57,090 >> So then we're in main. 1101 01:02:57,090 --> 01:03:00,340 Ensure user entered one and only one command line argument. 1102 01:03:00,340 --> 01:03:02,780 Just like our pseudocode, we have that if condition up here. 1103 01:03:02,780 --> 01:03:05,650 Hopefully, you guys are pretty familiar with this line and able to 1104 01:03:05,650 --> 01:03:10,590 write this code pretty quickly, checking argc. 1105 01:03:10,590 --> 01:03:12,430 >> Next, we're doing that a to i line. 1106 01:03:12,430 --> 01:03:13,490 Again, you've seen that before. 1107 01:03:13,490 --> 01:03:17,310 That's converting a string to an integer. 1108 01:03:17,310 --> 01:03:19,520 >> Next thing I do is I have another if condition. 1109 01:03:19,520 --> 01:03:22,450 I'm making sure they entered a positive number of dolphins per the 1110 01:03:22,450 --> 01:03:26,810 specification, checking if dolphins is less than 1. 1111 01:03:26,810 --> 01:03:31,090 So again, this first part of the code, these three things-- 1112 01:03:31,090 --> 01:03:33,300 check the number of args, n if it's greater than zero-- 1113 01:03:33,300 --> 01:03:35,770 you've written this code a lot, validating user input. 1114 01:03:35,770 --> 01:03:39,210 You should be pretty familiar with writing this code and being able to 1115 01:03:39,210 --> 01:03:41,100 write it pretty quickly. 1116 01:03:41,100 --> 01:03:43,220 >> Next, we're initializing a new array. 1117 01:03:43,220 --> 01:03:45,140 What type of things are we putting in this array? 1118 01:03:45,140 --> 01:03:48,950 1119 01:03:48,950 --> 01:03:53,010 Integers per this first three characters. 1120 01:03:53,010 --> 01:03:57,020 >> Next, we're getting ages, and a normal for loop iterating through an array, 1121 01:03:57,020 --> 01:04:00,405 and we're calling this function getAge, storing the return value of 1122 01:04:00,405 --> 01:04:02,000 getAge in the array. 1123 01:04:02,000 --> 01:04:04,630 Again, you've written code like this a lot before, too. 1124 01:04:04,630 --> 01:04:08,585 One thing that hopefully you will glean from going through your past 1125 01:04:08,585 --> 01:04:10,930 problems for this quiz is that a lot of what you're going to continue to do 1126 01:04:10,930 --> 01:04:12,730 are things you've already done. 1127 01:04:12,730 --> 01:04:15,820 It's very important to see those patterns so that you're not going to 1128 01:04:15,820 --> 01:04:18,660 make your work super hard for you in the future by thinking I've never done 1129 01:04:18,660 --> 01:04:19,050 this before. 1130 01:04:19,050 --> 01:04:21,400 You've done all of this stuff before. 1131 01:04:21,400 --> 01:04:23,880 >> Next, our final lines of code. 1132 01:04:23,880 --> 01:04:27,890 We're keeping track of the oldest age, going through each age, just like you 1133 01:04:27,890 --> 01:04:29,040 suggested, Alden. 1134 01:04:29,040 --> 01:04:33,060 And if the age of the current thing we're iterating through is greater 1135 01:04:33,060 --> 01:04:36,350 than the oldest number we have stored, we update the oldest number. 1136 01:04:36,350 --> 01:04:40,170 >> And finally at the end, we print out the oldest number. 1137 01:04:40,170 --> 01:04:41,340 Again, you've done this before. 1138 01:04:41,340 --> 01:04:43,850 You've actually written linear search. 1139 01:04:43,850 --> 01:04:44,980 You've written that. 1140 01:04:44,980 --> 01:04:46,190 It was a little different. 1141 01:04:46,190 --> 01:04:48,135 You were looking for a specific number, not the greatest one, but 1142 01:04:48,135 --> 01:04:50,300 you've written this code before. 1143 01:04:50,300 --> 01:04:52,930 >> And finally, in int getAge, you've seen this before. 1144 01:04:52,930 --> 01:04:55,780 We have a do-while loop asking for user input until it 1145 01:04:55,780 --> 01:04:57,080 meets a certain condition. 1146 01:04:57,080 --> 01:05:00,650 And finally, we're just returning that integer. 1147 01:05:00,650 --> 01:05:03,650 >> So all of this code you have seen before and written before. 1148 01:05:03,650 --> 01:05:05,250 Nothing Is new here. 1149 01:05:05,250 --> 01:05:08,040 Of course, I've presented it in a different way, and I maybe was too 1150 01:05:08,040 --> 01:05:11,050 confusing with my directions, but you've seen this before. 1151 01:05:11,050 --> 01:05:14,870 This logic you're 100% capable of. 1152 01:05:14,870 --> 01:05:18,670 And if we run this program really quickly-- 1153 01:05:18,670 --> 01:05:22,300 I've already made it, so we'll run ./trainer. 1154 01:05:22,300 --> 01:05:23,110 >> What happened? 1155 01:05:23,110 --> 01:05:24,878 >> STUDENT: [INAUDIBLE]. 1156 01:05:24,878 --> 01:05:26,120 >> JASON HIRSCHHORN: I didn't give any arguments. 1157 01:05:26,120 --> 01:05:27,290 Let's train five dolphins. 1158 01:05:27,290 --> 01:05:30,450 That would be one two, three, four, five years of age. 1159 01:05:30,450 --> 01:05:34,240 And as you can see right here, it printed out the oldest age. 1160 01:05:34,240 --> 01:05:35,580 So let's go back into gedit. 1161 01:05:35,580 --> 01:05:38,400 >> But of course, this wasn't what the specification was asking for. 1162 01:05:38,400 --> 01:05:40,870 It was asking the prototype. 1163 01:05:40,870 --> 01:05:45,070 Not the returns in integer, but the returns in int star. 1164 01:05:45,070 --> 01:05:47,600 So if I'm going to change it up there in the prototype I better 1165 01:05:47,600 --> 01:05:49,660 change it down here. 1166 01:05:49,660 --> 01:05:52,140 >> And now let's work through this, right here. 1167 01:05:52,140 --> 01:05:53,460 Let's start at the bottom. 1168 01:05:53,460 --> 01:05:54,720 Age is not an int star. 1169 01:05:54,720 --> 01:05:56,987 Age is what type of variable? 1170 01:05:56,987 --> 01:05:57,861 >> STUDENT: An integer. 1171 01:05:57,861 --> 01:06:02,240 >> JASON HIRSCHHORN: So line 63, age should not be an integer. 1172 01:06:02,240 --> 01:06:03,070 It should be an int star. 1173 01:06:03,070 --> 01:06:05,680 How do I make it an int star? 1174 01:06:05,680 --> 01:06:07,560 >> STUDENT: [INAUDIBLE]. 1175 01:06:07,560 --> 01:06:10,750 >> JASON HIRSCHHORN: Put a star there. 1176 01:06:10,750 --> 01:06:13,080 And now let's look down here. 1177 01:06:13,080 --> 01:06:20,070 67, getInt returns an integer, and we're storing that integer in age. 1178 01:06:20,070 --> 01:06:21,980 But if we're treating age as a pointer now, how do we need 1179 01:06:21,980 --> 01:06:24,224 to update this line? 1180 01:06:24,224 --> 01:06:25,926 >> STUDENT: Put a star. 1181 01:06:25,926 --> 01:06:28,876 >> JASON HIRSCHHORN: Put a star before age. 1182 01:06:28,876 --> 01:06:30,397 Is that what you said? 1183 01:06:30,397 --> 01:06:31,311 >> STUDENT: Yeah. 1184 01:06:31,311 --> 01:06:32,100 >> JASON HIRSCHHORN: Right. 1185 01:06:32,100 --> 01:06:34,340 Because getInt returns an integer. 1186 01:06:34,340 --> 01:06:39,110 We want to go to the location given to us in age and put an integer there. 1187 01:06:39,110 --> 01:06:42,420 If we did this line of code, we'd go back to what Curtis was talking about 1188 01:06:42,420 --> 01:06:46,170 earlier, saving an age, some crazy integer, maybe the integer 5. 1189 01:06:46,170 --> 01:06:48,860 Eventually we try to go to location 5 in memory, and we get in a lot of 1190 01:06:48,860 --> 01:06:51,040 trouble for that. 1191 01:06:51,040 --> 01:06:54,550 >> So we need that star there, and line 69? 1192 01:06:54,550 --> 01:06:57,260 Again, age, it's in memory address right here. 1193 01:06:57,260 --> 01:06:59,850 So how do we need to update this line? 1194 01:06:59,850 --> 01:07:00,990 Somebody else? 1195 01:07:00,990 --> 01:07:02,612 How do we need to update this line? 1196 01:07:02,612 --> 01:07:04,872 >> STUDENT: Put an asterisk there. 1197 01:07:04,872 --> 01:07:06,180 >> JASON HIRSCHHORN: Exactly right. 1198 01:07:06,180 --> 01:07:08,270 Add a little star right there. 1199 01:07:08,270 --> 01:07:12,060 Now we're getting the value pointed to by age rather than age. 1200 01:07:12,060 --> 01:07:15,090 >> So again, we saw this code go from integer to end point. 1201 01:07:15,090 --> 01:07:17,130 Pretty similar. 1202 01:07:17,130 --> 01:07:19,965 We need to look for all those places we had age, and we need to change them 1203 01:07:19,965 --> 01:07:23,180 a little bit because we don't want to change age's box so much as what age 1204 01:07:23,180 --> 01:07:26,470 is pointing to. 1205 01:07:26,470 --> 01:07:30,790 >> And returning age, is that correct? 1206 01:07:30,790 --> 01:07:32,900 That is correct, because age is an int pointer. 1207 01:07:32,900 --> 01:07:33,845 Yeah, question? 1208 01:07:33,845 --> 01:07:38,030 >> STUDENT: Can we keep it int age throughout, and then at the end, put 1209 01:07:38,030 --> 01:07:39,930 an ampersand in front of age? 1210 01:07:39,930 --> 01:07:41,780 >> JASON HIRSCHHORN: And return its address? 1211 01:07:41,780 --> 01:07:43,380 That's an excellent question. 1212 01:07:43,380 --> 01:07:48,720 You could do that, but let's get to that in a second and answer that 1213 01:07:48,720 --> 01:07:49,710 question in a second. 1214 01:07:49,710 --> 01:07:50,790 Fantastic question. 1215 01:07:50,790 --> 01:07:58,170 >> Up here, if we're turning an int pointer, what type of things is 1216 01:07:58,170 --> 01:08:00,460 dolphin ages storing in it? 1217 01:08:00,460 --> 01:08:03,640 If we're returning an int pointer right here, what type of thing is 1218 01:08:03,640 --> 01:08:06,380 dolphin ages storing in it? 1219 01:08:06,380 --> 01:08:10,220 Int pointers, so line 37 should be int star. 1220 01:08:10,220 --> 01:08:11,260 That's the type of this array. 1221 01:08:11,260 --> 01:08:12,370 It's not storing integers anymore. 1222 01:08:12,370 --> 01:08:13,620 It's storing int pointers. 1223 01:08:13,620 --> 01:08:16,340 1224 01:08:16,340 --> 01:08:22,750 >> And finally down here, dolphin ages bracket i, that's going to give us, 1225 01:08:22,750 --> 01:08:24,189 again, and int pointer. 1226 01:08:24,189 --> 01:08:25,590 We don't want an int pointer. 1227 01:08:25,590 --> 01:08:27,960 We want the value pointed to by that int. 1228 01:08:27,960 --> 01:08:29,500 So Hassan, how can we change this line? 1229 01:08:29,500 --> 01:08:30,939 >> STUDENT: Put a star? 1230 01:08:30,939 --> 01:08:32,899 >> JASON HIRSCHHORN: Exactly right, put a star right there. 1231 01:08:32,899 --> 01:08:33,750 That'll give us a value. 1232 01:08:33,750 --> 01:08:35,587 What about line 51? 1233 01:08:35,587 --> 01:08:37,050 >> STUDENT: Another star. 1234 01:08:37,050 --> 01:08:39,779 >> JASON HIRSCHHORN: Another star. 1235 01:08:39,779 --> 01:08:41,490 That's exactly right. 1236 01:08:41,490 --> 01:08:45,740 So we're treating, again now, these things as int pointers. 1237 01:08:45,740 --> 01:08:51,015 >> STUDENT: Is it a problem at all that in getAge, int star age is never given 1238 01:08:51,015 --> 01:08:51,899 e location? 1239 01:08:51,899 --> 01:08:54,910 >> JASON HIRSCHHORN: It is a problem that int star age is never given a 1240 01:08:54,910 --> 01:08:56,930 location, and it's going to be a similar problem to what you were 1241 01:08:56,930 --> 01:08:59,830 talking about with just returning ampersand age and 1242 01:08:59,830 --> 01:09:00,450 keeping it as an int. 1243 01:09:00,450 --> 01:09:02,580 But I want to run this code first and see what happens. 1244 01:09:02,580 --> 01:09:07,229 1245 01:09:07,229 --> 01:09:07,720 >> OK. 1246 01:09:07,720 --> 01:09:10,399 So variable age is unitialized when used here. 1247 01:09:10,399 --> 01:09:14,240 So it says, "initialize the variable 'age' to silence this warning," and it 1248 01:09:14,240 --> 01:09:16,630 suggests giving it null. 1249 01:09:16,630 --> 01:09:19,510 Let's give it null. 1250 01:09:19,510 --> 01:09:20,790 So we should get rid of tha warning. 1251 01:09:20,790 --> 01:09:22,040 We should be able to compile. 1252 01:09:22,040 --> 01:09:29,710 1253 01:09:29,710 --> 01:09:30,600 >> How old is the dolphin? 1254 01:09:30,600 --> 01:09:33,180 Let's say, 1. 1255 01:09:33,180 --> 01:09:36,240 Seg fault. 1256 01:09:36,240 --> 01:09:39,300 >> So I would go through GDB now, and I encourage you to go through GDB on 1257 01:09:39,300 --> 01:09:43,250 your own to figure out why this program just seg faulted. 1258 01:09:43,250 --> 01:09:45,580 I'm not going to leave this as a cliffhanger, though, and I'm going to 1259 01:09:45,580 --> 01:09:47,960 open up this function. 1260 01:09:47,960 --> 01:09:50,950 We're going to understand why this program seg faulted. 1261 01:09:50,950 --> 01:09:55,640 So as you all on the stack, when a function is called, a tray is added to 1262 01:09:55,640 --> 01:09:59,590 the proverbial stack where local variables are stored. 1263 01:09:59,590 --> 01:10:02,910 When a function returns, that tray goes away. 1264 01:10:02,910 --> 01:10:05,540 Those local variables go away as well. 1265 01:10:05,540 --> 01:10:11,330 >> Int star age equals null is a local variable saved in that stack frame. 1266 01:10:11,330 --> 01:10:16,570 When this function goes away, this variable goes away as well. 1267 01:10:16,570 --> 01:10:19,980 So we passed on the address of whatever. 1268 01:10:19,980 --> 01:10:22,540 We passed on age. 1269 01:10:22,540 --> 01:10:25,280 It's an address, we passed that on. 1270 01:10:25,280 --> 01:10:29,430 But ultimately, we-- 1271 01:10:29,430 --> 01:10:32,580 >> Sorry, I'm getting ahead of myself. 1272 01:10:32,580 --> 01:10:36,940 Why this program really seg faulted in this case was because age was pointing 1273 01:10:36,940 --> 01:10:40,560 to null, which is a specific location, and null is a location you're not 1274 01:10:40,560 --> 01:10:41,870 allowed to touch. 1275 01:10:41,870 --> 01:10:45,570 And so when we tried to write this line of code, going to age and doing 1276 01:10:45,570 --> 01:10:49,090 something there, we got in trouble, and that's where we seg faulted. 1277 01:10:49,090 --> 01:10:51,630 Because star age, age again is pointing to null. 1278 01:10:51,630 --> 01:10:53,450 We want to null, which is a bad area. 1279 01:10:53,450 --> 01:10:58,570 We tried to getInt there, 1 in our case, and that's where we seg faulted. 1280 01:10:58,570 --> 01:11:01,340 >> But say that an opt-in case, say that we had given this a legitimate address 1281 01:11:01,340 --> 01:11:05,800 right here, like bf4ac, something we saw before. 1282 01:11:05,800 --> 01:11:10,960 Even so, when this program returned, we would run into a problem because 1283 01:11:10,960 --> 01:11:13,810 that address would have gone away. 1284 01:11:13,810 --> 01:11:16,870 The local variable address would have gone away. 1285 01:11:16,870 --> 01:11:20,580 Whatever age was pointed to wouldn't have existed anymore. 1286 01:11:20,580 --> 01:11:22,090 >> The way we fix that is something you've seen 1287 01:11:22,090 --> 01:11:24,450 before, the malloc function. 1288 01:11:24,450 --> 01:11:26,240 And you've been introduced to that malloc function before. 1289 01:11:26,240 --> 01:11:27,670 You've seen this? 1290 01:11:27,670 --> 01:11:30,140 Malloc gives you some memory that doesn't go away when the 1291 01:11:30,140 --> 01:11:31,950 stack frame goes away. 1292 01:11:31,950 --> 01:11:33,660 So writing this line of code-- 1293 01:11:33,660 --> 01:11:35,360 and I'm going to give it to you for now-- 1294 01:11:35,360 --> 01:11:36,120 malloc. 1295 01:11:36,120 --> 01:11:41,490 And since we're storing an int, we're going to malloc enough space. 1296 01:11:41,490 --> 01:11:44,480 We're going to say give me a box of size int. 1297 01:11:44,480 --> 01:11:49,480 Malloc returns an address, and we're saving that address in age. 1298 01:11:49,480 --> 01:11:50,805 >> Now when we run this program-- 1299 01:11:50,805 --> 01:11:56,810 1300 01:11:56,810 --> 01:12:01,040 we're not going to seg fault because those blocks of memory we got through 1301 01:12:01,040 --> 01:12:05,140 malloc don't disappear when the stack frame disappears. 1302 01:12:05,140 --> 01:12:07,160 They live on until we want to get rid of them. 1303 01:12:07,160 --> 01:12:09,610 >> And this goes back to your question, Jeff, from earlier. 1304 01:12:09,610 --> 01:12:17,660 How do I create things inside of my functions that I want to use later on 1305 01:12:17,660 --> 01:12:19,310 in main or in different functions? 1306 01:12:19,310 --> 01:12:25,800 I use malloc, and I pass their addresses around. 1307 01:12:25,800 --> 01:12:30,200 >> That was a lot, admittedly, to cover just now. 1308 01:12:30,200 --> 01:12:33,380 So please, if you have any questions, ask away. 1309 01:12:33,380 --> 01:12:37,817 1310 01:12:37,817 --> 01:12:40,930 >> STUDENT: So malloc isn't on the stack anymore, then? 1311 01:12:40,930 --> 01:12:42,900 >> JASON HIRSCHHORN: That's exactly right. 1312 01:12:42,900 --> 01:12:46,620 The box malloc is giving you is not on the stack. 1313 01:12:46,620 --> 01:12:49,270 It's giving it to you in this other part of memory which we're going to 1314 01:12:49,270 --> 01:12:51,010 talk about in lecture on Wednesday. 1315 01:12:51,010 --> 01:12:54,650 The name is the heap, but I don't want to talk too much about it other than 1316 01:12:54,650 --> 01:12:57,920 it's this other place that doesn't get erased when the function goes away. 1317 01:12:57,920 --> 01:13:04,790 1318 01:13:04,790 --> 01:13:06,040 >> Any other questions? 1319 01:13:06,040 --> 01:13:08,999 1320 01:13:08,999 --> 01:13:13,390 >> STUDENT: I think if you didn't use a separate function and you put all this 1321 01:13:13,390 --> 01:13:16,705 code into main, would you still need to use malloc? 1322 01:13:16,705 --> 01:13:20,250 >> JASON HIRSCHHORN: Great question. 1323 01:13:20,250 --> 01:13:23,180 This is probably not the most efficient way of writing this program. 1324 01:13:23,180 --> 01:13:26,270 You could have easily done this do-while loop in main and saved 1325 01:13:26,270 --> 01:13:28,800 yourself a whole lot of trouble. 1326 01:13:28,800 --> 01:13:31,910 The reason why I wanted to do it like this is because I wanted everybody to 1327 01:13:31,910 --> 01:13:37,680 be able to see how in main, we can create an array of 1328 01:13:37,680 --> 01:13:41,060 pointers, int pointers. 1329 01:13:41,060 --> 01:13:45,590 We can initialize those pointers somewhere else, put them in this other 1330 01:13:45,590 --> 01:13:50,610 area of memory that stays for as long as we want it to stay, and then we can 1331 01:13:50,610 --> 01:13:51,500 access them later. 1332 01:13:51,500 --> 01:13:53,670 >> For example, here we searched through them. 1333 01:13:53,670 --> 01:13:54,770 We could have done other things to them. 1334 01:13:54,770 --> 01:13:56,820 We could have gotten them and found the average. 1335 01:13:56,820 --> 01:13:57,750 We could have done a lot of things. 1336 01:13:57,750 --> 01:14:02,650 The point being that you can keep track of some things in main through 1337 01:14:02,650 --> 01:14:05,820 an array, create those things somewhere else, and then still use 1338 01:14:05,820 --> 01:14:06,620 them later on. 1339 01:14:06,620 --> 01:14:09,910 >> And this idea, what we're doing now, is going to come up a lot in future 1340 01:14:09,910 --> 01:14:13,130 P-sets and problems you want to solve. 1341 01:14:13,130 --> 01:14:16,260 That's why we did it like this even though admittedly it's convoluted a 1342 01:14:16,260 --> 01:14:17,440 bit in this context. 1343 01:14:17,440 --> 01:14:20,004 >> STUDENT: [INAUDIBLE] 1344 01:14:20,004 --> 01:14:25,060 if you can keep all the int ages as actual values as opposed to pointers, 1345 01:14:25,060 --> 01:14:27,610 could you use the ampersand age now? 1346 01:14:27,610 --> 01:14:28,410 >> JASON HIRSCHHORN: Sorry? 1347 01:14:28,410 --> 01:14:32,100 >> STUDENT: If you took out the stars on all the ages, and just did return 1348 01:14:32,100 --> 01:14:34,610 ampersand age, at the end, would that work? 1349 01:14:34,610 --> 01:14:36,670 >> JASON HIRSCHHORN: So malloc returns an address. 1350 01:14:36,670 --> 01:14:37,752 >> STUDENT: OK, right. 1351 01:14:37,752 --> 01:14:41,550 >> JASON HIRSCHHORN: It returns an address, so age has to be an int 1352 01:14:41,550 --> 01:14:43,555 pointer, because it's going to be stored in an address. 1353 01:14:43,555 --> 01:14:52,980 1354 01:14:52,980 --> 01:14:55,160 >> We have six minutes left. 1355 01:14:55,160 --> 01:14:58,590 I'm going to skip talking about the stack for now. 1356 01:14:58,590 --> 01:15:04,090 I'm going to go into three minutes briefly on SPL, the most important 1357 01:15:04,090 --> 01:15:06,470 things I think you need to know for the problem set. 1358 01:15:06,470 --> 01:15:09,430 >> However, before I get here, because this is what we're going to finish on, 1359 01:15:09,430 --> 01:15:11,440 two final reminders. 1360 01:15:11,440 --> 01:15:14,640 One, admittedly we went through things pretty quickly in this section. 1361 01:15:14,640 --> 01:15:16,340 I apologize for that pace. 1362 01:15:16,340 --> 01:15:20,120 If you have any questions about something we went over, why something 1363 01:15:20,120 --> 01:15:22,810 worked the way it did, or something I do not explain as clearly as I could 1364 01:15:22,810 --> 01:15:26,830 have, please shoot me an email or talk to me after class, and I'll be happy 1365 01:15:26,830 --> 01:15:28,360 to explain it to you more in depth. 1366 01:15:28,360 --> 01:15:30,320 So please reach out if you have questions. 1367 01:15:30,320 --> 01:15:33,270 >> Again, the feedback website as well. 1368 01:15:33,270 --> 01:15:36,120 Take some time after class if you have it and give me some feedback. 1369 01:15:36,120 --> 01:15:37,730 I appreciate it a lot. 1370 01:15:37,730 --> 01:15:41,050 As you saw at the beginning of class, I read through them, respond to them 1371 01:15:41,050 --> 01:15:43,440 if you want a response, and I do you take that to heart. 1372 01:15:43,440 --> 01:15:45,250 That means a lot to me. 1373 01:15:45,250 --> 01:15:49,250 >> Let's finish class by talking about the library you're going to 1374 01:15:49,250 --> 01:15:50,200 be using this week. 1375 01:15:50,200 --> 01:15:53,220 You've probably seen code like what I have on the screen up before, GRect 1376 01:15:53,220 --> 01:15:54,470 paddle equals newGRect,(x, y, width, height). 1377 01:15:54,470 --> 01:15:57,410 1378 01:15:57,410 --> 01:16:00,350 You guys have probably through reading through the spec felt pretty 1379 01:16:00,350 --> 01:16:03,530 comfortable with this, but for those who were less comfortable, for you 1380 01:16:03,530 --> 01:16:06,830 guys as well, I want to re-go over this line and see how it's similar to 1381 01:16:06,830 --> 01:16:09,010 what we've used before. 1382 01:16:09,010 --> 01:16:11,610 >> Just like with any variable-- 1383 01:16:11,610 --> 01:16:14,570 this is an object, but just like with any variable, we have the type first 1384 01:16:14,570 --> 01:16:17,490 and foremost right here, GRect. 1385 01:16:17,490 --> 01:16:22,600 >> Next we have the name of the object, and finally we have the function that 1386 01:16:22,600 --> 01:16:27,820 initializes it, gives us the paddle we want. 1387 01:16:27,820 --> 01:16:32,040 Sometimes we write int x equals 5, and we initialize something directly. 1388 01:16:32,040 --> 01:16:34,930 Sometimes we write string name equals get string. 1389 01:16:34,930 --> 01:16:40,110 We use a function to get something, and we take its return value, and 1390 01:16:40,110 --> 01:16:40,880 that's our new value. 1391 01:16:40,880 --> 01:16:44,580 >> So we've seen this idea before, where functions that may or may not need 1392 01:16:44,580 --> 01:16:47,050 input actually create the thing for us, rather than 1393 01:16:47,050 --> 01:16:49,390 us creating it directly. 1394 01:16:49,390 --> 01:16:51,060 So we've seen this code before. 1395 01:16:51,060 --> 01:16:55,490 Hopefully, this idea of an object, or at least creating an object, is not as 1396 01:16:55,490 --> 01:16:59,550 overwhelming if you can conceptualize it as just very similar to things 1397 01:16:59,550 --> 01:17:01,160 we've seen before. 1398 01:17:01,160 --> 01:17:04,280 >> However, the cool thing about objects as distinct from variables is that we 1399 01:17:04,280 --> 01:17:06,770 can do things with them. 1400 01:17:06,770 --> 01:17:09,920 You can't change the color of a variable, but you can indeed change 1401 01:17:09,920 --> 01:17:13,250 the color of an object, or at least in SPL you can change the 1402 01:17:13,250 --> 01:17:14,540 color of your objects. 1403 01:17:14,540 --> 01:17:18,540 >> You can't move a variable in a window, but you can move 1404 01:17:18,540 --> 01:17:20,100 an object in a window. 1405 01:17:20,100 --> 01:17:21,450 You can get an object's location. 1406 01:17:21,450 --> 01:17:23,110 You can't really get a variable's location. 1407 01:17:23,110 --> 01:17:26,070 That's the cool thing about objects in this library. 1408 01:17:26,070 --> 01:17:29,040 >> However, these functions can be kind of confusing. 1409 01:17:29,040 --> 01:17:33,410 We haven't really used functions or objects before, so I felt it was 1410 01:17:33,410 --> 01:17:38,420 helpful to understand a generic way to express or do things with objects, or 1411 01:17:38,420 --> 01:17:39,740 understand objects. 1412 01:17:39,740 --> 01:17:41,940 And as you'll notice, I have two functions written here, functions 1413 01:17:41,940 --> 01:17:45,400 you've seen before-- add, window comma paddle, and set color 1414 01:17:45,400 --> 01:17:47,070 paddle comma black. 1415 01:17:47,070 --> 01:17:49,810 In both these cases, we have a function that takes two arguments and 1416 01:17:49,810 --> 01:17:51,280 does something. 1417 01:17:51,280 --> 01:17:54,080 >> Now more generically, all of these functions you're going to write that 1418 01:17:54,080 --> 01:17:59,760 have to deal with objects take a where and a what. 1419 01:17:59,760 --> 01:18:03,870 A where is the object you want to do something to, in this 1420 01:18:03,870 --> 01:18:06,150 case, a window or panel. 1421 01:18:06,150 --> 01:18:09,700 And the what is what you want to do it or give to it. 1422 01:18:09,700 --> 01:18:11,140 >> When you're setting the color something, where are 1423 01:18:11,140 --> 01:18:11,980 you setting the color? 1424 01:18:11,980 --> 01:18:14,160 With the paddle object. 1425 01:18:14,160 --> 01:18:16,230 And what color are you setting? 1426 01:18:16,230 --> 01:18:18,320 Black. 1427 01:18:18,320 --> 01:18:20,610 With the add function, where are you doing something? 1428 01:18:20,610 --> 01:18:21,610 With the window object. 1429 01:18:21,610 --> 01:18:22,630 And what are you giving to it? 1430 01:18:22,630 --> 01:18:24,230 You're giving it the paddle. 1431 01:18:24,230 --> 01:18:29,070 >> You'll notice that the functions you use, set color, set location, et 1432 01:18:29,070 --> 01:18:32,140 cetera, et cetera, all follow this pattern. 1433 01:18:32,140 --> 01:18:35,180 They take something you want to do something to, the object you want to 1434 01:18:35,180 --> 01:18:39,970 do something to, and what you want to do with it, or what you want to have 1435 01:18:39,970 --> 01:18:42,630 changed, or what you want to have added, or the specific color 1436 01:18:42,630 --> 01:18:43,430 you want it to be. 1437 01:18:43,430 --> 01:18:49,330 Or if it's not filled, do you want to make it filled, et cetera, et cetera. 1438 01:18:49,330 --> 01:18:53,500 >> Admittedly, a very brief discussion of the Stanford-- 1439 01:18:53,500 --> 01:18:55,950 what is it? 1440 01:18:55,950 --> 01:18:59,060 Stanford Programming Library? 1441 01:18:59,060 --> 01:18:59,890 Portable Library, that's right! 1442 01:18:59,890 --> 01:19:01,340 Thank you, class. 1443 01:19:01,340 --> 01:19:04,150 Stanford Portable Library. 1444 01:19:04,150 --> 01:19:06,710 But again, a lot more information is given in the problem set spec, but 1445 01:19:06,710 --> 01:19:09,680 hopefully this makes it a little bit easier to understand object, 1446 01:19:09,680 --> 01:19:13,710 especially as they've been relating to things we've seen and done before. 1447 01:19:13,710 --> 01:19:16,010 >> So with that, that is week four. 1448 01:19:16,010 --> 01:19:18,140 Enjoy learning about the heap tomorrow. 1449 01:19:18,140 --> 01:19:22,360 Good luck on your quiz next week, but of course, I will see before then, 1450 01:19:22,360 --> 01:19:24,860 hopefully in section next Tuesday. 1451 01:19:24,860 --> 01:19:27,970 But also if you have any questions, comments, concerns, please feel free 1452 01:19:27,970 --> 01:19:28,930 to reach out. 1453 01:19:28,930 --> 01:19:31,745 And after class, I'll step outside if you want to talk about your problem 1454 01:19:31,745 --> 01:19:32,995 set or have some questions. 1455 01:19:32,995 --> 01:19:35,915