1 00:00:01,090 --> 00:00:03,010 Pointer arithmetic. 2 00:00:03,310 --> 00:00:11,710 Here, a pointer points to memory and the type of the pointer determines the type of the data that can 3 00:00:11,710 --> 00:00:13,630 be accessed through the pointer. 4 00:00:13,660 --> 00:00:23,260 So here so for example, an integer type pointer will point to an integer in memory and you dereference 5 00:00:23,260 --> 00:00:26,680 the pointer to get the integer. 6 00:00:26,680 --> 00:00:32,970 So if the pointer allows it, you can change its value through pointer arithmetic. 7 00:00:32,980 --> 00:00:36,820 So for example, you can increment or decrement the pointer. 8 00:00:36,820 --> 00:00:43,420 So what happens to the value of the memory address depends on the type of pointer So assigns type. 9 00:00:43,420 --> 00:00:46,510 The pointer points to a type. 10 00:00:46,510 --> 00:00:52,030 Any pointer will change the pointer in units of the size of that type. 11 00:00:52,030 --> 00:01:01,240 So if you increment the integer pointer, it will point to the next integer in memory and change in 12 00:01:01,240 --> 00:01:03,970 the memory address depends on the size of the integer. 13 00:01:03,970 --> 00:01:06,880 So this is equivalent to array indexing here. 14 00:01:06,880 --> 00:01:16,270 So where an expression such as for example v one means you should start at the memory location of the 15 00:01:16,270 --> 00:01:25,210 first item in the V and then move one item further in memory and return the item here. 16 00:01:25,210 --> 00:01:30,490 So let's create an V variable here or let's create an just A here. 17 00:01:30,490 --> 00:01:38,950 So we're going to assign it just an integer, but we will create an array for example, one, two, 18 00:01:38,950 --> 00:01:40,690 three, four and five. 19 00:01:41,110 --> 00:01:51,070 And here we're going to create a pointer, a here named point P A, and we'll assign this to a and I 20 00:01:51,070 --> 00:01:58,810 will explain all this code here, of course, as I do always and here pointer array, the reference 21 00:01:58,810 --> 00:02:07,640 point array and here 11 assign it 11 and here a one year 12. 22 00:02:08,810 --> 00:02:09,590 And. 23 00:02:11,230 --> 00:02:11,950 To. 24 00:02:13,020 --> 00:02:19,770 To here equals assign it 13 and hear the reference. 25 00:02:19,770 --> 00:02:28,410 Put the referencing operator here and PA plus three and assign it 14 here for example. 26 00:02:29,400 --> 00:02:35,190 So let's run our code before of course, we have to write the print statements here. 27 00:02:36,010 --> 00:02:41,950 So the first line here allocates an array of five integers. 28 00:02:42,810 --> 00:02:50,190 On the stack and initializes the values of the number from from 1 to 5. 29 00:02:50,280 --> 00:02:54,110 In this example we have an initialization list is used. 30 00:02:54,120 --> 00:03:01,220 The compiler will create a space for the required number of items and the size of the array is not given. 31 00:03:01,230 --> 00:03:08,010 So if you give the size of the array between the bracelets here, then the initialization list must 32 00:03:08,010 --> 00:03:11,400 not have more items than the array size. 33 00:03:11,400 --> 00:03:19,950 So if the list has fewer items, then the rest of the items in the array are initialized to the default 34 00:03:19,950 --> 00:03:22,410 value, usually zero. 35 00:03:22,500 --> 00:03:25,020 So the next line in this code here. 36 00:03:25,880 --> 00:03:29,990 Obtains a pointer to the first items in the array. 37 00:03:29,990 --> 00:03:35,000 So this lines after array items in various ways. 38 00:03:35,000 --> 00:03:44,300 So the first of these a changes the first item in the lines, first item in the array by the referencing 39 00:03:44,300 --> 00:03:47,900 the pointer and assigning it a value here. 40 00:03:47,900 --> 00:03:49,340 The second here. 41 00:03:50,000 --> 00:03:50,330 Um. 42 00:03:53,410 --> 00:03:54,760 Yeah, let me hear. 43 00:03:54,790 --> 00:04:03,820 The second year uses indexing, but this time with a pointer and assigns a value to the third value 44 00:04:03,820 --> 00:04:04,880 in array. 45 00:04:04,900 --> 00:04:13,330 So and the final example here, as you can see here, this expression uses a pointer arithmetic to determine 46 00:04:13,330 --> 00:04:16,630 the address of the fourth item in the array. 47 00:04:16,660 --> 00:04:20,850 So remember, the first item was index of zero here. 48 00:04:20,860 --> 00:04:26,800 So and the reference is the pointer to assign the item value after this. 49 00:04:26,800 --> 00:04:29,260 The array contains here. 50 00:04:29,600 --> 00:04:30,790 Uh, here. 51 00:04:30,790 --> 00:04:31,440 After. 52 00:04:31,480 --> 00:04:38,320 After all of this here array contains 11, 12, 13, 14 and five. 53 00:04:39,650 --> 00:04:40,520 So. 54 00:04:41,360 --> 00:04:45,740 Now I want to illustrate this for more example. 55 00:04:45,740 --> 00:04:51,950 So actually, let me create a diagram for it and I will show you what we did here. 56 00:04:55,910 --> 00:04:56,650 Here. 57 00:04:56,660 --> 00:05:03,440 This is an example of what we did here with this code here. 58 00:05:06,580 --> 00:05:10,540 So basically this code, we will write it here. 59 00:05:10,870 --> 00:05:21,700 Does this illustration as I diagram as I draw it here so you can watch and just illustrate it by your 60 00:05:21,700 --> 00:05:22,090 own. 61 00:05:22,090 --> 00:05:26,500 So if you have a memory buffer, which now we're going to. 62 00:05:27,690 --> 00:05:32,190 Try the second example here with C plus plus pointers. 63 00:05:33,030 --> 00:05:42,330 So if you have a memory buffer containing values in this in this example, I'll look at an array and 64 00:05:42,330 --> 00:05:44,640 you want to multiply each value by three. 65 00:05:44,670 --> 00:05:48,970 You can do using the pointer arithmetic. 66 00:05:48,990 --> 00:05:51,930 So let's create an example here. 67 00:05:52,860 --> 00:05:57,060 So make this comment. 68 00:05:58,620 --> 00:05:59,370 For. 69 00:05:59,370 --> 00:06:01,660 We will use the for loop here. 70 00:06:01,680 --> 00:06:05,190 Integer or integer A. 71 00:06:06,490 --> 00:06:08,690 Actually, let's create a new integer here. 72 00:06:08,710 --> 00:06:09,970 Integer. 73 00:06:16,300 --> 00:06:16,960 Camp here. 74 00:06:19,170 --> 00:06:23,100 Or we can create an integer in this function integer. 75 00:06:23,310 --> 00:06:25,550 Here, zero. 76 00:06:25,560 --> 00:06:27,030 We will assign it first to zero. 77 00:06:27,030 --> 00:06:39,060 And while it's less than five, then increment integer by one here plus plus E here. 78 00:06:40,100 --> 00:06:48,290 And then we're going to use the pointer arithmetic to increase its value by one here. 79 00:06:49,210 --> 00:06:58,120 So the loop statement is complicated and you will need to refer back to the operator precedence in this 80 00:06:58,120 --> 00:06:59,260 lecture here. 81 00:06:59,260 --> 00:07:05,350 So the postfix increment operator has the highest precedence. 82 00:07:05,770 --> 00:07:09,250 The highest precedence is the reference operator here. 83 00:07:09,250 --> 00:07:14,200 And finally this operator has the lowest of the three operators. 84 00:07:14,200 --> 00:07:17,290 So the operations are run in order. 85 00:07:17,290 --> 00:07:18,490 The first. 86 00:07:20,050 --> 00:07:22,270 Then this. 87 00:07:23,290 --> 00:07:24,970 And lastly this. 88 00:07:25,180 --> 00:07:30,400 So the postfix operator returns the value before the increment. 89 00:07:30,400 --> 00:07:32,590 So of the pointer is incremented. 90 00:07:32,590 --> 00:07:34,930 So the next item in memory. 91 00:07:34,960 --> 00:07:39,790 The expression uses the address before the increment. 92 00:07:39,790 --> 00:07:47,230 So this address is the referenced which is assigned by the assignment operator that replaces the item 93 00:07:47,230 --> 00:07:50,600 with the value multiplied by three. 94 00:07:50,620 --> 00:07:55,780 So this illustrates an important dereference between pointers and array names. 95 00:07:55,780 --> 00:08:01,600 So you can increment a pointer, but you cannot increment an array. 96 00:08:01,600 --> 00:08:09,610 So let's so you can do this here, for example, P plus equals increment by one statement here. 97 00:08:09,610 --> 00:08:17,950 And you can do this with pointers, but you cannot do this with arrays here. 98 00:08:19,140 --> 00:08:19,740 One. 99 00:08:22,620 --> 00:08:25,740 Cannot hear or error here. 100 00:08:26,880 --> 00:08:33,930 As you can see here, you will get an error because invalid operands to binary expressions. 101 00:08:38,080 --> 00:08:47,530 And actually you can of course use the indexing with this both on arrays and pointers here. 102 00:08:51,130 --> 00:08:52,440 Let's test comment. 103 00:08:52,740 --> 00:09:00,540 If you download this code in the lecture section and you can use it, of course, and you can play with 104 00:09:00,540 --> 00:09:03,360 these pointers in your own time.