1 00:00:00,000 --> 00:00:00,340 2 00:00:00,340 --> 00:00:01,960 >> SPEAKER 1: Let's now fix that last program. 3 00:00:01,960 --> 00:00:04,920 And this time, let's explicitly allocate some memory in which the 4 00:00:04,920 --> 00:00:06,550 user's input will be stored. 5 00:00:06,550 --> 00:00:09,600 To do so, let's hone in on that first line of code where we declared s 6 00:00:09,600 --> 00:00:11,590 previously to be a char star. 7 00:00:11,590 --> 00:00:14,210 This time, let's re-declare it as follows-- 8 00:00:14,210 --> 00:00:19,380 char s bracket 16, for instance, close bracket. 9 00:00:19,380 --> 00:00:23,690 >> In other words, let's declare s to no longer be an address of a character, 10 00:00:23,690 --> 00:00:26,610 but rather an array of 16 characters. 11 00:00:26,610 --> 00:00:30,295 This way, the user can type in up to 15 characters and still leave room for 12 00:00:30,295 --> 00:00:31,570 a null terminator. 13 00:00:31,570 --> 00:00:35,870 Let's proceed to Save, Compile, and Run this program. 14 00:00:35,870 --> 00:00:40,770 Make scanf2 dot slash scanf2. 15 00:00:40,770 --> 00:00:45,100 Let's now type a string like hello, and we're thanked for the hello. 16 00:00:45,100 --> 00:00:46,440 >> Now, there's still a problem. 17 00:00:46,440 --> 00:00:50,140 I only typed in hello, which is only five characters, plus 1 for the null 18 00:00:50,140 --> 00:00:50,445 terminator. 19 00:00:50,445 --> 00:00:53,610 It leaves us with only a need for six bytes. 20 00:00:53,610 --> 00:00:56,740 >> But unfortunately, we only allocated 16 in total. 21 00:00:56,740 --> 00:01:01,305 So if the user actually types in 16 characters, or 17, or several hundred 22 00:01:01,305 --> 00:01:04,410 characters, we're still not going to have enough room in memory for the 23 00:01:04,410 --> 00:01:05,400 user's input. 24 00:01:05,400 --> 00:01:07,750 And in fact, this is what makes getting user input so 25 00:01:07,750 --> 00:01:08,940 difficult in general. 26 00:01:08,940 --> 00:01:12,270 And indeed, this is why we implemented the get string function in the first 27 00:01:12,270 --> 00:01:13,900 place in the CS50 library-- 28 00:01:13,900 --> 00:01:16,900 to figure out how to handle those situations where the user types in 29 00:01:16,900 --> 00:01:19,710 more characters than we initially anticipated. 30 00:01:19,710 --> 00:01:21,750 >> Frankly, without completely rewriting this program, 31 00:01:21,750 --> 00:01:23,290 there's no clean solution. 32 00:01:23,290 --> 00:01:26,970 Rather, what we would have to do is get a character from the user one at a 33 00:01:26,970 --> 00:01:28,860 time, again and again. 34 00:01:28,860 --> 00:01:32,510 And at each point if we realize we're out of memory, we would have to at 35 00:01:32,510 --> 00:01:36,450 that point go back in and reallocate some more memory, copy the user's 36 00:01:36,450 --> 00:01:39,400 previous input from our first chunk of memory into the new, 37 00:01:39,400 --> 00:01:40,810 larger chunk of memory. 38 00:01:40,810 --> 00:01:44,610 And then repeat that process again and again until the user is done providing 39 00:01:44,610 --> 00:01:45,860 his or her input. 40 00:01:45,860 --> 00:01:48,246