1 00:00:05,300 --> 00:00:08,130 In this video, we'll see how we can do pass-by-reference 2 00:00:08,130 --> 00:00:09,520 using pointer parameters. 3 00:00:10,179 --> 00:00:13,580 In c++, we can use pointers and the de-reference operator 4 00:00:13,799 --> 00:00:15,460 to achieve pass-by-reference. 5 00:00:16,619 --> 00:00:19,780 In this case, the function parameter is a pointer, and the actual 6 00:00:19,780 --> 00:00:22,880 parameter can be a pointer variable or the address of a variable. 7 00:00:23,350 --> 00:00:25,340 Let's see what this looks like syntactically. 8 00:00:28,580 --> 00:00:30,569 Here we're declaring a function prototype and 9 00:00:30,570 --> 00:00:31,760 then defining the function. 10 00:00:32,179 --> 00:00:33,779 The function is called double data. 11 00:00:34,119 --> 00:00:36,700 And it expects a pointer to an integer as a parameter. 12 00:00:37,080 --> 00:00:38,700 Notice it doesn't return anything. 13 00:00:39,610 --> 00:00:42,970 Instead, it will double the contents of the data the pointer points to. 14 00:00:43,410 --> 00:00:46,520 So in the body of the function, we simply de-reference the pointer 15 00:00:46,790 --> 00:00:50,620 and multiply the value points 2 by 2, and assign it right back. 16 00:00:51,080 --> 00:00:54,140 I've written this using a compound assignment statement, but I've also 17 00:00:54,140 --> 00:00:56,700 written out the long form of the assignment statement just in case 18 00:00:56,700 --> 00:00:58,480 the compound assignment is confusing. 19 00:00:59,210 --> 00:01:03,429 I also chose to multiply by 2 on purpose since this uses the asterisk 20 00:01:03,429 --> 00:01:05,080 as the multiplication operator. 21 00:01:05,590 --> 00:01:08,320 Keep them straight, don't let the syntax confuse you. 22 00:01:08,440 --> 00:01:11,890 In one case, the asterisk is multiplication, and in the other, it's 23 00:01:11,900 --> 00:01:13,720 indirection or de-reference operator. 24 00:01:14,010 --> 00:01:15,780 Okay, so how do you call this function. 25 00:01:15,880 --> 00:01:17,190 Let's see that in the next slide. 26 00:01:17,720 --> 00:01:20,179 We know that since the function parameter is a pointer to an 27 00:01:20,179 --> 00:01:23,140 integer, that means it expects the address of an integer. 28 00:01:23,460 --> 00:01:25,730 Well, in this case, I have an integer named value and 29 00:01:25,730 --> 00:01:26,920 it's initialized to 10. 30 00:01:27,320 --> 00:01:29,979 I can call double data with the address of that integer 31 00:01:29,990 --> 00:01:32,680 using the ampersand, which is the address of operator. 32 00:01:33,340 --> 00:01:37,440 When I return from the function, I now display value which has doubled to 20. 33 00:01:38,130 --> 00:01:41,060 Let's head over to the IDE and see this and other examples of 34 00:01:41,060 --> 00:01:42,730 pass-by-reference with pointers. 35 00:01:44,130 --> 00:01:45,360 Okay, so I'm in the IDE. 36 00:01:45,360 --> 00:01:48,110 And you can see that I'm in the section 12 workspace in 37 00:01:48,110 --> 00:01:50,270 the passing pointers 1 project. 38 00:01:50,580 --> 00:01:53,750 We'll go over a couple of different projects that do pass-by-reference 39 00:01:54,010 --> 00:01:55,906 using pointers a little bit differently from one another. 40 00:01:55,906 --> 00:01:58,250 So you can see exactly what's happening, and I'll walk 41 00:01:58,260 --> 00:01:59,440 you through these in detail. 42 00:01:59,730 --> 00:02:02,300 You can see here that we've got the same function that we wrote in the 43 00:02:02,300 --> 00:02:03,589 slides, right here, double data. 44 00:02:04,529 --> 00:02:06,189 It expects a pointer to an integer. 45 00:02:06,529 --> 00:02:10,080 And all it does is it doubles whatever it's pointing to, right. 46 00:02:10,080 --> 00:02:12,420 So whatever integer is pointing, it was just doubling it. 47 00:02:12,750 --> 00:02:14,370 And here's my main, which we're going to run. 48 00:02:14,450 --> 00:02:16,270 We're going to run main couple different ways. 49 00:02:16,290 --> 00:02:19,610 First, this part right here, and then the second part right here. 50 00:02:20,330 --> 00:02:23,719 This, you'll see is use the address of a value, and this is using an actual 51 00:02:23,719 --> 00:02:26,989 pointer, so it's two different ways to pass really the same information in. 52 00:02:27,260 --> 00:02:28,840 Okay, so let's start running this. 53 00:02:28,840 --> 00:02:31,710 We've got our main, and it's activated on the stack, right here. 54 00:02:32,210 --> 00:02:34,249 You'll notice main has two local variables. 55 00:02:34,510 --> 00:02:37,150 It's got an integer value, and it's got an integer pointer. 56 00:02:37,730 --> 00:02:40,739 So the value is on the stack. 57 00:02:41,000 --> 00:02:42,680 And right now, it's initialized to 10. 58 00:02:42,680 --> 00:02:48,679 And we also have an int pointer, which is a pointer to an integer. 59 00:02:48,680 --> 00:02:49,820 And right now, it's null and. 60 00:02:49,820 --> 00:02:50,799 I'll just leave it blank. 61 00:02:52,010 --> 00:02:53,579 Let's say that value is in address 1000. 62 00:02:55,450 --> 00:02:58,629 And in pointer is that address 2000, just to keep things straight. 63 00:02:58,940 --> 00:03:00,120 Okay, so that's where we're at. 64 00:03:01,049 --> 00:03:03,369 Now we're here at line 16, output value. 65 00:03:03,960 --> 00:03:04,640 Well, what's value? 66 00:03:04,930 --> 00:03:08,050 Well, we just go to the variable and grab with its contents, right. 67 00:03:08,050 --> 00:03:10,379 So we're going to display a 10 here, just like we have before. 68 00:03:12,170 --> 00:03:16,140 Now we call the function double data, and we pass in the address of value. 69 00:03:16,140 --> 00:03:18,644 Okay, so main stops what it's doing, and we transfer control 70 00:03:18,870 --> 00:03:20,050 over to this function up here. 71 00:03:20,710 --> 00:03:25,140 So we're going to call double data, and we're going to 72 00:03:25,140 --> 00:03:27,640 pass in the address of value. 73 00:03:27,640 --> 00:03:30,280 You can see right here, what's the address of value, it's a 1000. 74 00:03:30,969 --> 00:03:32,999 So we're gonna pass in a 1000 to this function. 75 00:03:34,369 --> 00:03:37,459 Okay, that 1000 will get copied into end pointer. 76 00:03:37,470 --> 00:03:41,260 So that function right here has a local variable, called int pointer. 77 00:03:42,140 --> 00:03:43,829 And you can see that guy right there. 78 00:03:44,620 --> 00:03:46,919 Notice it's the same name as this variable. 79 00:03:46,940 --> 00:03:49,820 That's okay, we can use the same name for different variables. 80 00:03:50,059 --> 00:03:52,340 These are two different variables and two different scopes. 81 00:03:52,529 --> 00:03:54,970 Okay, so we're passing in a 1000 to it. 82 00:03:55,110 --> 00:03:56,410 So that's what we're doing. 83 00:03:56,910 --> 00:03:58,150 There's our pointer. 84 00:03:58,990 --> 00:04:02,370 So now that int pointer is pointing to this value, 85 00:04:02,370 --> 00:04:03,720 right here at address 1000. 86 00:04:05,270 --> 00:04:06,010 So what do we do now? 87 00:04:06,020 --> 00:04:08,210 Well, we're going to double that value basically. 88 00:04:08,210 --> 00:04:11,345 So let me write this out longhand here, so we'll de-reference int 89 00:04:11,440 --> 00:04:16,190 pointer on the left-hand side equals again the reference int 90 00:04:16,260 --> 00:04:20,969 pointer times 2, and that's what this compound assignment is doing. 91 00:04:21,389 --> 00:04:24,870 Notice that, that asterisk right there is multiplication. 92 00:04:25,280 --> 00:04:28,720 These asterisks here, that's the the reference operator. 93 00:04:29,000 --> 00:04:31,830 Keep them straight. It doesn't matter that you're asked, just look at the context, 94 00:04:31,830 --> 00:04:32,840 and it should be pretty clear. 95 00:04:33,160 --> 00:04:34,550 All right, so let's execute this. 96 00:04:35,350 --> 00:04:38,360 De-reference in pointer follow the point to where it's pointing, 97 00:04:38,929 --> 00:04:43,820 right here I got a 10, multiply that by 2, gives me the 20. 98 00:04:43,820 --> 00:04:47,830 And we're assigning 20 into wherever int pointer is pointing. 99 00:04:47,830 --> 00:04:49,609 Well, int pointer is pointing to a 1000. 100 00:04:49,920 --> 00:04:52,800 So we just change that 10 to a 20. 101 00:04:53,600 --> 00:04:56,310 Okay, at this point, that function is done, so we're 102 00:04:56,310 --> 00:04:57,480 going to clean up the stack. 103 00:04:57,510 --> 00:05:00,940 This activation gets popped off the stack, so this is all gone now. 104 00:05:02,960 --> 00:05:03,659 There you go. 105 00:05:03,740 --> 00:05:05,499 Now we're back to main. 106 00:05:06,129 --> 00:05:07,600 And we output the value. 107 00:05:07,690 --> 00:05:13,640 The value is now 20, and we're done with that function call. 108 00:05:13,730 --> 00:05:15,190 Okay, so now let's keep going. 109 00:05:16,000 --> 00:05:16,860 We're here now. 110 00:05:16,940 --> 00:05:18,540 We're going to output a bunch of lines. 111 00:05:18,910 --> 00:05:22,599 And then we're setting int pointer to the address of value. 112 00:05:23,109 --> 00:05:24,870 Well, int pointer was null before. 113 00:05:24,880 --> 00:05:26,470 Remember, we nulled it out right here. 114 00:05:26,470 --> 00:05:29,560 So at this point, int pointer now is the address of value. 115 00:05:29,560 --> 00:05:30,640 What's the address of value? 116 00:05:30,640 --> 00:05:31,510 It's a 1000. 117 00:05:31,520 --> 00:05:32,490 So it's a 1000. 118 00:05:34,380 --> 00:05:35,390 There's my pointer. 119 00:05:35,840 --> 00:05:39,109 I'm pointing to value so. 120 00:05:39,110 --> 00:05:39,860 What do I do now? 121 00:05:39,910 --> 00:05:41,760 I call double data again. 122 00:05:43,539 --> 00:05:44,980 So let's call double data. 123 00:05:45,360 --> 00:05:46,940 And what are we passing into it this time? 124 00:05:46,940 --> 00:05:47,599 Int pointer. 125 00:05:47,600 --> 00:05:49,210 Well, what's the value of int pointer? 126 00:05:49,290 --> 00:05:50,210 A 1000. 127 00:05:50,810 --> 00:05:54,630 So we're calling this with a 1000, which is the address of some integer. 128 00:05:55,040 --> 00:05:58,650 So we activate that on the that activation record, and 129 00:05:58,650 --> 00:06:01,360 it's got that same local variable, right, int pointer. 130 00:06:02,510 --> 00:06:06,660 That's this guy right up here, and we're copying a 1000 in. 131 00:06:07,930 --> 00:06:10,580 There's the pointer, same as we had before. 132 00:06:10,830 --> 00:06:11,630 So what do we do now? 133 00:06:11,630 --> 00:06:12,829 We execute this code. 134 00:06:13,200 --> 00:06:15,200 It now changes this 20 to a 40. 135 00:06:17,020 --> 00:06:20,970 Okay, when the function is done, we clean up our stack, we pop that 136 00:06:20,970 --> 00:06:22,600 activation record off the stack. 137 00:06:23,810 --> 00:06:25,450 So this pointer is gone. 138 00:06:25,950 --> 00:06:28,719 And then we come right back to main, and we say output to value. 139 00:06:28,719 --> 00:06:29,880 Well, the value is now 40. 140 00:06:31,290 --> 00:06:32,240 Okay, so there you go. 141 00:06:32,299 --> 00:06:34,569 This is how pass-by-reference with pointers works. 142 00:06:34,570 --> 00:06:35,940 It's really pretty straightforward. 143 00:06:36,059 --> 00:06:42,040 Let's execute this code now, and you'll see the result is -- and I'll 144 00:06:42,040 --> 00:06:45,560 slide this over just a little bit, you can see value here was 10, then 145 00:06:45,630 --> 00:06:49,419 we doubled it to 20, then we called it again and doubled it again to 40. 146 00:06:50,240 --> 00:06:50,979 All right, perfect. 147 00:06:50,980 --> 00:06:52,770 So let's take a look at another example now. 148 00:06:56,059 --> 00:06:57,630 Okay, so here's another example. 149 00:06:57,680 --> 00:06:59,760 I'm in the same workspace, section 12. 150 00:07:00,540 --> 00:07:02,800 Now I'm in the passingPointers2 project. 151 00:07:03,730 --> 00:07:06,770 And what I've done here is I've written a swap function, that 152 00:07:06,770 --> 00:07:10,440 basically swaps 2 variables, 2 integer variables in this case. 153 00:07:10,459 --> 00:07:12,200 And here's my main that exercises it. 154 00:07:12,209 --> 00:07:13,550 So let's let's try this out. 155 00:07:14,180 --> 00:07:15,030 Here's my main. 156 00:07:15,430 --> 00:07:16,980 As usual, here's the stack. 157 00:07:17,380 --> 00:07:18,940 Main has 2 local variables. 158 00:07:18,950 --> 00:07:21,290 It's got x and it's got y. 159 00:07:21,889 --> 00:07:26,599 X is initialized to 100, and y is initialized to 200. 160 00:07:27,540 --> 00:07:30,259 Let's assume that desired address is 1000 and 2000. 161 00:07:31,850 --> 00:07:34,430 So we've got some nice easy numbers to work with. 162 00:07:35,030 --> 00:07:37,890 Okay, so right here, we're outputting what's x? 163 00:07:37,910 --> 00:07:39,900 We're putting a new line in front of x, but that's okay. 164 00:07:39,900 --> 00:07:40,620 What's x? 165 00:07:41,030 --> 00:07:42,120 Well, x is 100. 166 00:07:42,830 --> 00:07:44,620 So we're going to display 100 here. 167 00:07:45,960 --> 00:07:47,769 And now we're going to say, well, what's y? 168 00:07:47,949 --> 00:07:51,580 We're going to output the value of y which is 200, pretty easy. 169 00:07:52,990 --> 00:07:56,769 Now main stops what it's doing, and we transfer control over to swap. 170 00:07:57,120 --> 00:07:58,909 Okay, so we're going to call swap. 171 00:08:01,440 --> 00:08:04,140 And you'll notice swap has 3 local variables. 172 00:08:04,150 --> 00:08:07,300 It's got an a, it's got a b, and it's got a temp. 173 00:08:07,460 --> 00:08:10,419 So we need to allocate storage for all of those on the activation 174 00:08:10,420 --> 00:08:11,900 record on the stack, right. 175 00:08:11,950 --> 00:08:18,200 So we've got a, we've got b, and we've got temp. 176 00:08:19,040 --> 00:08:21,830 In this case, a and b are both pointers to integers and temp 177 00:08:21,830 --> 00:08:23,050 is just a regular integer. 178 00:08:23,389 --> 00:08:26,800 Okay, so we're going to call swap with the address of x and the address of y. 179 00:08:27,300 --> 00:08:32,169 The address of x is a 1000, and the address of y is 2000. 180 00:08:34,889 --> 00:08:37,219 So that's what the call looks like, right. 181 00:08:37,480 --> 00:08:42,279 Now this x, right here, right, gets mapped to that parameter 182 00:08:42,320 --> 00:08:43,500 a, so that's what we're doing. 183 00:08:43,510 --> 00:08:45,440 We're copying that value to a. 184 00:08:45,470 --> 00:08:47,000 So a is now going to be. 185 00:08:47,370 --> 00:08:49,350 What x is right here, right. 186 00:08:49,350 --> 00:08:53,699 So a is going to get a 1000, and b will get 2000. 187 00:08:54,809 --> 00:08:55,990 So here are your pointers. 188 00:08:56,320 --> 00:09:00,610 B is pointing right here, and a is pointing right there. 189 00:09:02,140 --> 00:09:04,130 All right, so now let's start executing this code. 190 00:09:05,160 --> 00:09:08,380 Temp, we're assigning what a points to, to temp. 191 00:09:08,710 --> 00:09:09,750 Well, what is a point to? 192 00:09:09,750 --> 00:09:11,300 We know it points to an integer, right, because 193 00:09:11,300 --> 00:09:12,359 this is an integer pointer. 194 00:09:12,710 --> 00:09:14,970 So we're gonna take the integer that a points to. 195 00:09:15,400 --> 00:09:17,624 Let's follow a, it points to a 100. 196 00:09:17,624 --> 00:09:19,040 And we're storing it in temp. 197 00:09:19,080 --> 00:09:20,630 Temp is an integer, not a pointer. 198 00:09:21,390 --> 00:09:22,510 Okay, so that's done. 199 00:09:23,120 --> 00:09:25,000 Now we're going to execute the second one which says, 200 00:09:25,260 --> 00:09:26,680 take what b is pointing to. 201 00:09:26,680 --> 00:09:27,890 All right, let's follow b. 202 00:09:28,010 --> 00:09:32,039 B is pointing to 200, and put it into where a was pointing. 203 00:09:32,099 --> 00:09:33,499 Okay, a was pointing here. 204 00:09:34,029 --> 00:09:35,700 So we're going to put the 200 here. 205 00:09:37,180 --> 00:09:38,350 So that's done. 206 00:09:38,700 --> 00:09:41,180 And then we'll take the temp, which was 100 right here. 207 00:09:41,180 --> 00:09:43,380 And we're going to put it into where b was pointing. 208 00:09:43,780 --> 00:09:46,420 So b was pointing to here. 209 00:09:46,800 --> 00:09:48,259 So we're going to put the 100 here. 210 00:09:48,259 --> 00:09:50,430 Notice those two integers got swapped now. 211 00:09:51,910 --> 00:09:54,250 Okay, so at this point, the function's done. 212 00:09:54,610 --> 00:09:55,310 What do we do? 213 00:09:55,470 --> 00:09:58,660 We clean up the stack, we pop off that activation record. 214 00:10:00,080 --> 00:10:01,439 All this is now gone. 215 00:10:02,500 --> 00:10:04,290 And we just clean these up as well. 216 00:10:05,590 --> 00:10:07,000 And that's where we're at right now. 217 00:10:07,210 --> 00:10:08,660 So we're back in main now. 218 00:10:08,870 --> 00:10:09,560 What's x? 219 00:10:09,560 --> 00:10:11,479 We print out, the value of x is 200. 220 00:10:12,270 --> 00:10:15,120 And we display the value of y right here, which is 100. 221 00:10:16,420 --> 00:10:18,730 Notice that we actually swapped those variables. 222 00:10:19,630 --> 00:10:22,209 Okay, so that's really what's going on with pass-by-reference 223 00:10:22,210 --> 00:10:23,970 with pointers in this example. 224 00:10:24,200 --> 00:10:25,429 It's pretty straightforward. 225 00:10:25,450 --> 00:10:27,179 If you remember, we did this with references. 226 00:10:28,379 --> 00:10:30,870 References is probably what you'd want to do here. 227 00:10:31,050 --> 00:10:35,470 But I'll talk about using references versus pointers in the very last 228 00:10:35,470 --> 00:10:39,249 video in this section, that gives you some insight as to what to use when. 229 00:10:39,550 --> 00:10:40,569 But there you go. 230 00:10:40,570 --> 00:10:41,750 This is how you do it with pointers. 231 00:10:41,770 --> 00:10:43,330 Let's look at another example now. 232 00:10:49,270 --> 00:10:56,910 Okay, so I've switched over to the PassingPointers3 project 233 00:10:57,010 --> 00:10:59,415 in the section 12 workspace. 234 00:10:59,860 --> 00:11:03,500 In this case, we're going to pass a pointer to a vector rather than using 235 00:11:03,500 --> 00:11:05,130 integers as we've done in the past. 236 00:11:05,379 --> 00:11:07,600 And there's a couple of examples in this main, and I'll leave the 237 00:11:07,600 --> 00:11:11,170 second example here that's commented out for after I do this one. 238 00:11:11,310 --> 00:11:12,970 So this is what I've got. 239 00:11:13,350 --> 00:11:17,829 I've got a real simple function here called display, and it expects 240 00:11:18,120 --> 00:11:20,380 a pointer to a vector of string. 241 00:11:20,380 --> 00:11:24,090 So it expects the address of some vector that holds strings. 242 00:11:24,469 --> 00:11:26,690 Okay, And all it's doing is just looping through the vector and 243 00:11:26,690 --> 00:11:28,250 printing out all the strings in it. 244 00:11:28,410 --> 00:11:29,910 I'm not going to draw the stack out now. 245 00:11:29,920 --> 00:11:31,850 I think this is going to be pretty easy to follow now that. 246 00:11:31,850 --> 00:11:33,230 I've done over all those examples. 247 00:11:33,420 --> 00:11:35,760 So in this case, I've got a vector of string objects. 248 00:11:36,139 --> 00:11:39,480 It's called stooges, and here's the 3 stooges: larry Moe and Curly. 249 00:11:40,379 --> 00:11:43,930 So what have I got, I've basically got a vector object. 250 00:11:44,360 --> 00:11:49,140 And it's got 3 strings: Larry Moe and Curly. 251 00:11:51,750 --> 00:12:00,069 It's called stooges, and it exists at location 1000 on the 252 00:12:00,070 --> 00:12:03,280 stack there, okay, in the main in mains activation record. 253 00:12:03,620 --> 00:12:05,250 Then I'm just calling stooges here. 254 00:12:05,320 --> 00:12:07,860 I'm calling display, and I'm passing in the address of stooges. 255 00:12:08,010 --> 00:12:08,770 Perfect. 256 00:12:08,920 --> 00:12:12,810 So now once I'm here in this display function that you see here 257 00:12:12,810 --> 00:12:17,679 on line 7, once I'm here, all I want to do is display all those 258 00:12:17,680 --> 00:12:19,250 strings that are in the vector. 259 00:12:19,370 --> 00:12:22,530 So I'm going to use a range base for loop that just iterates through the 260 00:12:22,530 --> 00:12:23,860 vector and prints out the strings. 261 00:12:24,080 --> 00:12:25,310 Well, I have a pointer. 262 00:12:25,320 --> 00:12:26,479 Here's my v, right. 263 00:12:27,000 --> 00:12:29,439 And v has a 1000 in it. 264 00:12:29,570 --> 00:12:31,600 So this is v, right here, it's pointing. 265 00:12:31,600 --> 00:12:32,939 V is not the vector again. 266 00:12:32,940 --> 00:12:34,640 V is just a simple pointer. 267 00:12:35,039 --> 00:12:38,380 So in order to get to what the pointer is pointing to, 268 00:12:38,420 --> 00:12:39,880 I de-reference the pointer. 269 00:12:39,880 --> 00:12:40,820 That's what I need to do. 270 00:12:41,140 --> 00:12:45,969 So if I de-reference v, I've got this stooges vector right here. 271 00:12:46,389 --> 00:12:48,859 And that's what's going on right there. 272 00:12:50,040 --> 00:12:52,449 So for auto, which is a string, right. 273 00:12:52,910 --> 00:12:55,380 So for every string in the collection. 274 00:12:55,380 --> 00:12:59,090 Well, the collection is not v, these the pointer to the collection. 275 00:12:59,380 --> 00:13:01,880 So if I de-reference v, I get the collection. 276 00:13:02,740 --> 00:13:04,960 And this executes just like you would expect. 277 00:13:06,059 --> 00:13:09,000 Okay, now let me clear this out, and I'll run it real 278 00:13:09,000 --> 00:13:11,259 quick so that we can test this. 279 00:13:12,750 --> 00:13:14,300 And there you see: Larry Moe and Curly 280 00:13:14,300 --> 00:13:16,332 being displayed by this function right here. 281 00:13:16,332 --> 00:13:20,249 Now in this case, we really don't want this display 282 00:13:20,249 --> 00:13:21,949 function to modify stuff, right. 283 00:13:21,949 --> 00:13:24,079 I mean it's only displaying vector elements, we don't 284 00:13:24,079 --> 00:13:25,399 want it modifying anything. 285 00:13:25,399 --> 00:13:27,575 So we could get this to modify something. 286 00:13:27,575 --> 00:13:31,170 That's not what we want to do, but we certainly could. 287 00:13:31,620 --> 00:13:35,460 To remember that we could do the at method of the vector class, 288 00:13:35,570 --> 00:13:42,310 well, we could simply do star v, right, de-reference this pointer, 289 00:13:42,320 --> 00:13:49,800 that gives me the vector, then I could say .at 0 equals funny. 290 00:13:51,100 --> 00:13:53,870 And what that will do Is it'll change the first vector 291 00:13:53,890 --> 00:13:55,939 element, Larry to Funny. 292 00:13:57,230 --> 00:13:58,179 Notice the syntax. 293 00:13:58,179 --> 00:13:59,869 The syntax may be a little strange. 294 00:14:00,139 --> 00:14:02,710 But think about this, this is very logical, right. 295 00:14:03,120 --> 00:14:06,340 If i de-reference the pointer, I get the vector. 296 00:14:06,340 --> 00:14:10,480 And then once I have the vector, I can do .at, .size 297 00:14:10,520 --> 00:14:11,710 whatever else I need on it. 298 00:14:11,910 --> 00:14:13,320 So this sets that to funny. 299 00:14:13,710 --> 00:14:20,639 I could also come over here and do something like v, 300 00:14:20,639 --> 00:14:21,869 which is the pointer, right. 301 00:14:22,690 --> 00:14:23,549 There's null pointer. 302 00:14:24,859 --> 00:14:26,340 So now I'm not pointing anywhere. 303 00:14:26,790 --> 00:14:29,040 So if I do both of these, you can see that this will 304 00:14:29,040 --> 00:14:30,430 compile just fine and run. 305 00:14:30,820 --> 00:14:38,260 Now if I move this to the top right there, now I'm actually going 306 00:14:38,260 --> 00:14:39,600 to change that vector element. 307 00:14:39,600 --> 00:14:42,670 So when I run it, you'll see funny now instead of Larry, 308 00:14:42,740 --> 00:14:43,599 you see that right there. 309 00:14:44,830 --> 00:14:47,410 So this is not something you want to happen, right. 310 00:14:47,410 --> 00:14:48,720 It's a display function. 311 00:14:48,720 --> 00:14:51,240 It shouldn't be messing around with the vector that you're pointing to. 312 00:14:51,490 --> 00:14:54,750 So in this case, we could use the const qualifier here, 313 00:14:55,820 --> 00:14:58,350 right, that will prevent this from happening right there. 314 00:14:58,350 --> 00:15:01,209 So if I do this now, I'll get a compiler error right there. 315 00:15:01,210 --> 00:15:03,860 You can see the red arrow saying, hey you're telling me that, 316 00:15:04,220 --> 00:15:06,680 that vector I'm pointing to is constant, you can't mess with it, 317 00:15:06,680 --> 00:15:07,939 you just tried to mess with it. 318 00:15:08,270 --> 00:15:11,159 So that won't be allowed. 319 00:15:11,559 --> 00:15:17,749 And if I put a const here, then this line 13 won't be allowed. 320 00:15:17,760 --> 00:15:19,600 I'll try to compile that, and there's the error. 321 00:15:19,880 --> 00:15:22,120 Because now you're telling me that the pointer is constant, but 322 00:15:22,160 --> 00:15:23,830 you just tried to change it here. 323 00:15:25,260 --> 00:15:27,570 Okay, so again that's not what you want to do here. 324 00:15:27,580 --> 00:15:32,989 All right, so now it's a lot safer this function. 325 00:15:33,190 --> 00:15:35,890 We can't change the vector, and we can't change the pointer either. 326 00:15:37,090 --> 00:15:40,770 So let's look at one last quick example, and I'll clear this out. 327 00:15:41,690 --> 00:15:44,110 And what we've got now is let me close this up. 328 00:15:44,110 --> 00:15:46,090 We've got another function called display because we're 329 00:15:46,090 --> 00:15:47,649 using function overloading here. 330 00:15:47,650 --> 00:15:50,270 Remember, we can use the same name for the for two different functions. 331 00:15:50,910 --> 00:15:55,210 And let me comment this piece out, and I'll uncomment this piece right here. 332 00:15:56,950 --> 00:16:00,339 Okay, so in this case, I have an array of scores, right, 333 00:16:00,380 --> 00:16:01,515 which is an array of integers. 334 00:16:01,770 --> 00:16:05,080 And I've set them all to these scores, and I've got a negative 1 at the end. 335 00:16:05,679 --> 00:16:07,829 In this case, the negative 1 is a sentinel. 336 00:16:08,020 --> 00:16:10,240 This is where I want to stop processing. 337 00:16:10,850 --> 00:16:14,300 So what I'm doing in this display function here is I'm passing in the 338 00:16:14,300 --> 00:16:18,960 address of that first integer and the sentinel, where do I want to stop. 339 00:16:19,250 --> 00:16:23,619 And you can see what this is doing, it's saying while what I'm pointing 340 00:16:23,620 --> 00:16:28,359 to right there is not equal to the sentinel, which, in this case, is -1. 341 00:16:29,619 --> 00:16:32,370 Then process what I'm doing, right. 342 00:16:32,370 --> 00:16:36,085 So I'm going to basically de-reference that array, display a 343 00:16:36,085 --> 00:16:39,809 100 and then increment the array, the pointer to the next element. 344 00:16:39,809 --> 00:16:41,290 So think about what's going on here. 345 00:16:41,290 --> 00:16:42,189 Here's the array. 346 00:16:43,190 --> 00:16:52,600 And let's just put a few numbers in here: 100 98 97 79 85 and -1. 347 00:16:52,930 --> 00:16:54,610 I might have missed one, but that's okay. 348 00:16:54,970 --> 00:16:57,769 So at this point, that's my scores array. 349 00:16:58,000 --> 00:16:59,080 This is scores. 350 00:17:00,080 --> 00:17:03,550 This is, let's say, address 1000, right. 351 00:17:03,860 --> 00:17:04,329 Perfect. 352 00:17:04,348 --> 00:17:06,128 So now I'm passing in scores. 353 00:17:06,529 --> 00:17:11,010 So now my array variable in the function points right there. 354 00:17:12,069 --> 00:17:13,829 And my sentinel is -1. 355 00:17:14,199 --> 00:17:19,230 All right, so am I pointing to negative 1? 356 00:17:19,469 --> 00:17:20,170 No, I'm not. 357 00:17:20,190 --> 00:17:21,098 I'm pointing to a 100. 358 00:17:21,480 --> 00:17:27,339 All right, print out 100, and increment the pointer. 359 00:17:27,420 --> 00:17:28,680 Where's the pointer pointing to? 360 00:17:28,680 --> 00:17:31,719 Well, now the pointer is going to point here, not here. 361 00:17:32,679 --> 00:17:35,209 So now we're going to loop again and print out a 98. 362 00:17:35,570 --> 00:17:37,350 And we're going to keep doing that until we hit -1. 363 00:17:37,959 --> 00:17:43,340 So this is going to print 98 97 79 85 and then it's going to stop. 364 00:17:44,570 --> 00:17:49,690 In this case, we could pass in constant for the array, right. 365 00:17:49,690 --> 00:17:51,649 We're pointing to these numbers, we don't want to change it. 366 00:17:51,679 --> 00:17:54,539 But we are actually updating the pointer here. 367 00:17:55,200 --> 00:17:58,619 So we can't have a constant pointer, right. 368 00:17:58,660 --> 00:18:02,500 We can have constant a pointer constant integers, but not a 369 00:18:02,500 --> 00:18:05,160 constant pointers because we're actually changing the pointer. 370 00:18:05,420 --> 00:18:07,820 This is something, in this example here, that you 371 00:18:07,820 --> 00:18:09,130 can't do with references. 372 00:18:09,130 --> 00:18:11,479 You've really got to use pointers to do this kind of stuff. 373 00:18:12,650 --> 00:18:14,680 Okay, so that wraps up this video. 374 00:18:15,370 --> 00:18:18,020 There you see a couple of different examples of pass 375 00:18:18,020 --> 00:18:19,260 by value with pointers. 376 00:18:19,830 --> 00:18:22,570 At this point, you might be thinking do I use pointers, do I 377 00:18:22,570 --> 00:18:24,320 use references, how do I do this? 378 00:18:24,650 --> 00:18:27,699 Stay tuned. In the last video in this section, I'll tell you exactly 379 00:18:27,870 --> 00:18:30,260 when you should use pointers and when you should use references.