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