1 00:00:00,000 --> 00:00:03,080 >> SPEAKER 1: It turns out we can successfully swap the values in two 2 00:00:03,080 --> 00:00:07,670 variables by passing them into a function not by value or by copy, but 3 00:00:07,670 --> 00:00:10,390 by reference or by their addresses. 4 00:00:10,390 --> 00:00:12,740 In other words, we need to leverage something known as a pointer. 5 00:00:12,740 --> 00:00:15,580 A pointer, really, is just the address of some variable. 6 00:00:15,580 --> 00:00:19,660 And so if we provide a function called, say, swap with the address of 7 00:00:19,660 --> 00:00:23,550 a variable and the address of another variable, swap should be empowered to 8 00:00:23,550 --> 00:00:26,090 go to each of those addresses and actually change the 9 00:00:26,090 --> 00:00:27,360 values that are there. 10 00:00:27,360 --> 00:00:28,890 >> Let's see this in context. 11 00:00:28,890 --> 00:00:31,360 Let's reimplement swap as follows. 12 00:00:31,360 --> 00:00:35,810 First, let's change a not to be an int but to be a pointer to an int or the 13 00:00:35,810 --> 00:00:36,920 address of an int. 14 00:00:36,920 --> 00:00:40,820 Then let's do the same for b, changing it from an int to be a pointer to an 15 00:00:40,820 --> 00:00:42,780 int or the address of an int. 16 00:00:42,780 --> 00:00:45,860 >> Then inside of swap, let's still declare tmp so that we have a 17 00:00:45,860 --> 00:00:47,810 temporary place for a's value. 18 00:00:47,810 --> 00:00:52,430 But a's value is not a itself, because, again, a is now the address 19 00:00:52,430 --> 00:00:53,270 of some int. 20 00:00:53,270 --> 00:00:57,320 So if we want to go to that address and get int at that address, we have 21 00:00:57,320 --> 00:01:03,020 dereference this pointer, also by way of the star operator, writing star a. 22 00:01:03,020 --> 00:01:05,470 >> Next, I don't want to change the value of a. 23 00:01:05,470 --> 00:01:08,770 I want to change the value at a, keeping in mind, again, 24 00:01:08,770 --> 00:01:10,350 that a is an address. 25 00:01:10,350 --> 00:01:14,050 So to do so, I again need to say star a gets. 26 00:01:14,050 --> 00:01:18,360 And now I want to put in the value that's at b, not the value of b, which 27 00:01:18,360 --> 00:01:19,720 also is an address. 28 00:01:19,720 --> 00:01:22,280 >> So again I say, star b. 29 00:01:22,280 --> 00:01:26,690 Then in my last line, I need to overwrite what is at address b with 30 00:01:26,690 --> 00:01:28,970 what was at a's original location. 31 00:01:28,970 --> 00:01:32,910 To do that, I do star b gets tmp. 32 00:01:32,910 --> 00:01:34,820 >> Now at the end of the day, this function is still just 33 00:01:34,820 --> 00:01:35,950 three lines of code. 34 00:01:35,950 --> 00:01:39,860 But because it's manipulating values by way of their address and not the 35 00:01:39,860 --> 00:01:43,700 raw values that were passed into the function, I claim that swap is now 36 00:01:43,700 --> 00:01:47,670 empowered to change the values that are passed in via their addresses. 37 00:01:47,670 --> 00:01:49,510 >> But I need to make one change still. 38 00:01:49,510 --> 00:01:52,190 I can no longer pass in x and y themselves. 39 00:01:52,190 --> 00:01:55,030 I need to pass in the addresses of x and y. 40 00:01:55,030 --> 00:01:58,160 And to do that, I need some slightly different notation up top. 41 00:01:58,160 --> 00:02:02,510 I want to swap x and y by passing in the address of x, indicated by 42 00:02:02,510 --> 00:02:07,190 ampersand x, and the address of y, indicated by ampersand y. 43 00:02:07,190 --> 00:02:10,570 >> Similarly, up top now do I need to change the function's prototype to 44 00:02:10,570 --> 00:02:14,980 match the change that I've made, so that a is, again, a pointer to an int. 45 00:02:14,980 --> 00:02:17,190 b is, again, a pointer to an int. 46 00:02:17,190 --> 00:02:18,770 And now I can save my file. 47 00:02:18,770 --> 00:02:20,680 And let's recompile and run it. 48 00:02:20,680 --> 00:02:25,330 >> Make swap dot slash swap. 49 00:02:25,330 --> 00:02:29,660 And this time, x and y are indeed now swapped such that their values are not 50 00:02:29,660 --> 00:02:31,950 1 and 2, but 2 and 1. 51 00:02:31,950 --> 00:02:34,900