1 00:00:00,000 --> 00:00:00,410 2 00:00:00,410 --> 00:00:03,130 >> DAVID J. MALAN: So it turns out that copying a string is not nearly as 3 00:00:03,130 --> 00:00:05,750 simple as copying a primitive, like an int or a float. 4 00:00:05,750 --> 00:00:09,190 After all, underneath the hood a string is a sequence characters. 5 00:00:09,190 --> 00:00:13,130 So copying a string, therefore, has to involve copying that whole sequence of 6 00:00:13,130 --> 00:00:14,240 characters. 7 00:00:14,240 --> 00:00:17,470 >> Let's turn our attention back to that last implementation and rip out this 8 00:00:17,470 --> 00:00:21,470 line, string t equals s, which clearly wasn't sufficient. 9 00:00:21,470 --> 00:00:24,440 Let's replace it with a line that looks, instead, like this. 10 00:00:24,440 --> 00:00:34,020 String t gets malloc of string length of s plus 1 times the size of a char. 11 00:00:34,020 --> 00:00:36,320 >> Now there's quote a bit going on in this line of code. 12 00:00:36,320 --> 00:00:39,330 First, malloc, short for memory allocation, and the 13 00:00:39,330 --> 00:00:40,700 function does just that. 14 00:00:40,700 --> 00:00:44,740 Given an integer, it returns to you the address of a chunk of memory of 15 00:00:44,740 --> 00:00:45,960 that many bytes. 16 00:00:45,960 --> 00:00:50,090 Meanwhile, the string length of s plus 1 is meant to indicate that we want as 17 00:00:50,090 --> 00:00:54,690 many bytes as s already occupies, including its null terminator, the 18 00:00:54,690 --> 00:00:57,050 backslash 0 at the end of a string. 19 00:00:57,050 --> 00:01:00,170 >> Meanwhile, I don't necessarily remember how big a char is, even 20 00:01:00,170 --> 00:01:04,340 though on most systems it's simply 1 byte, so I'll call size of char to 21 00:01:04,340 --> 00:01:08,210 figure out dynamically how big an individual character is. 22 00:01:08,210 --> 00:01:12,550 Once multiplied together, I get back the total number of bytes that I need. 23 00:01:12,550 --> 00:01:14,680 >> But what if malloc fails to return the memory we need? 24 00:01:14,680 --> 00:01:16,730 I'd best check for that as follows. 25 00:01:16,730 --> 00:01:23,330 If t equals null, then I'm first going to free s, the memory returned by get 26 00:01:23,330 --> 00:01:27,120 string, and then I'm going to return 1, to signify error. 27 00:01:27,120 --> 00:01:30,360 >> But if all is well, I'm going to proceed to use a four loop and iterate 28 00:01:30,360 --> 00:01:31,110 as follows. 29 00:01:31,110 --> 00:01:36,000 For int i get 0, n equals the string length of s. 30 00:01:36,000 --> 00:01:40,350 I'm going to do this so long as i is less than or equal to n so that I 31 00:01:40,350 --> 00:01:44,460 iterate up through and including the null terminating character in s. 32 00:01:44,460 --> 00:01:47,450 >> And on each iteration, I'm going to increment i. 33 00:01:47,450 --> 00:01:52,496 Meanwhile, inside of this loop, copy s's i-th character into t's i-th 34 00:01:52,496 --> 00:01:59,310 location, it suffices to do t bracket i gets s bracket i. 41 00:01:59,320 --> 00:02:02,750 >> Let's now save, compile, and run this new program. 42 00:02:02,750 --> 00:02:06,690 Make copy 1 dot slash copy 1. 43 00:02:06,690 --> 00:02:09,460 And I'll say something like hello in all lowercase. 44 00:02:09,460 --> 00:02:12,280 And thankfully, this time my original remains unchanged. 45 00:02:12,280 --> 00:02:13,660 hello in all lowercase. 46 00:02:13,660 --> 00:02:15,540 But the copy is, indeed, capitalized. 47 00:02:37,120 --> 00:02:38,963