1 00:00:00,000 --> 00:00:00,520 2 00:00:00,520 --> 00:00:03,530 >> SPEAKER: Let's write a program that prompts the user for two strings and 3 00:00:03,530 --> 00:00:07,170 then reports whether those strings are the same or not the same. 4 00:00:07,170 --> 00:00:10,290 I've already started us off here by calling printf twice and calling 5 00:00:10,290 --> 00:00:14,520 GetString twice, storing the return values in s and t, respectively. 6 00:00:14,520 --> 00:00:17,960 >> Now, my instincts to compare these two strings would be to use the familiar 7 00:00:17,960 --> 00:00:19,160 equality operator-- 8 00:00:19,160 --> 00:00:22,070 if s equals equals t. 9 00:00:22,070 --> 00:00:28,120 Then I'm going to go ahead and print out "You typed the same thing! 10 00:00:28,120 --> 00:00:35,190 Else, if that's not true, I'm simply going to type printf("You typed 11 00:00:35,190 --> 00:00:37,880 different things! 12 00:00:37,880 --> 00:00:38,850 >> Fairly straightforward-- 13 00:00:38,850 --> 00:00:41,820 I'm simply comparing s against t, and if they're equal, 14 00:00:41,820 --> 00:00:43,250 printing out as much. 15 00:00:43,250 --> 00:00:45,450 Let's compile and run this program. 16 00:00:45,450 --> 00:00:51,950 Make compare 0 ./ compare 0, say something, hello, 17 00:00:51,950 --> 00:00:54,200 say something, hello. 18 00:00:54,200 --> 00:00:56,870 >> Unfortunately, the program thinks I've typed different things, even though I 19 00:00:56,870 --> 00:00:59,530 clearly typed "hello" the same way both times. 20 00:00:59,530 --> 00:01:00,850 Now, why might that be? 21 00:01:00,850 --> 00:01:03,750 >> Well, it turns out that all of this time, strings have been a bit more 22 00:01:03,750 --> 00:01:06,780 complex than a sequence of characters underneath the hood. 23 00:01:06,780 --> 00:01:11,450 In reality, a string is a pointer or an address, specifically the address 24 00:01:11,450 --> 00:01:14,640 of the first character in that sequence of characters. 25 00:01:14,640 --> 00:01:18,640 >> And so when we compare s against t with the equal equal sign, we're 26 00:01:18,640 --> 00:01:23,200 actually asking, is this address equal equal to this address? 27 00:01:23,200 --> 00:01:26,850 And that's not going to be the case if the user has typed in two different 28 00:01:26,850 --> 00:01:30,370 strings and we've called GetString twice to get them, because the memory 29 00:01:30,370 --> 00:01:34,480 that GetString uses to store the first string might be here in RAM, but the 30 00:01:34,480 --> 00:01:37,120 memory that GetString uses to store the second string is going 31 00:01:37,120 --> 00:01:38,760 to be here in RAM. 32 00:01:38,760 --> 00:01:42,380 And of course, then, those two chunks of memory have different addresses for 33 00:01:42,380 --> 00:01:44,220 their very first characters. 34 00:01:44,220 --> 00:01:46,120 >> So is s equal equal to t? 35 00:01:46,120 --> 00:01:46,885 Well, no. 36 00:01:46,885 --> 00:01:50,510 If s and t are pointing to different chunks of memory, as they would be by 37 00:01:50,510 --> 00:01:54,140 calling GetString twice, they're not, in fact, going to be the same. 38 00:01:54,140 --> 00:01:57,700 So it seems to be the case that to compare two strings in the intuitive 39 00:01:57,700 --> 00:02:01,050 way that we expect, character for character, we need another technique 40 00:02:01,050 --> 00:02:02,300 altogether. 41 00:02:02,300 --> 00:02:03,902