1 00:00:00,680 --> 00:00:07,370 Pointers share several characteristics with arrays, so pointers include object location. 2 00:00:07,400 --> 00:00:12,680 Arrays encode the location and the length of the contiguous objects. 3 00:00:12,710 --> 00:00:20,270 At the slightest provocation, an array will decay into a pointer, so a decayed array loses length 4 00:00:20,270 --> 00:00:24,920 information and converts the pointer to the Array's first element. 5 00:00:24,920 --> 00:00:28,220 So, for example, I want to write some example here. 6 00:00:28,880 --> 00:00:38,150 My array here and we will create an array and we will put three numbers in it six, three, six and 7 00:00:38,150 --> 00:00:40,520 nine here. 8 00:00:41,150 --> 00:00:43,850 Then we will create an integer here. 9 00:00:43,850 --> 00:00:47,900 Integer pointer key here. 10 00:00:48,290 --> 00:00:48,890 Key. 11 00:00:50,920 --> 00:00:52,450 Or Right. 12 00:00:52,520 --> 00:00:53,030 Ray. 13 00:00:53,050 --> 00:00:54,670 Peter here. 14 00:00:55,700 --> 00:00:57,070 Um, my array. 15 00:00:58,340 --> 00:00:59,150 That's the Mary. 16 00:00:59,360 --> 00:01:04,370 So this will point to three the and let's print it. 17 00:01:07,210 --> 00:01:10,030 Here we will use the add the reference operator. 18 00:01:11,810 --> 00:01:12,890 Okay. 19 00:01:13,890 --> 00:01:14,970 Raptor. 20 00:01:16,790 --> 00:01:17,840 And print it. 21 00:01:18,200 --> 00:01:18,620 Oops. 22 00:01:19,040 --> 00:01:19,670 Sorry. 23 00:01:20,780 --> 00:01:21,440 Here. 24 00:01:21,590 --> 00:01:23,780 We will get the pointer here as well. 25 00:01:23,780 --> 00:01:27,530 But I want to print it as an array here. 26 00:01:28,290 --> 00:01:31,670 And as you can see here, we got the three here. 27 00:01:32,040 --> 00:01:33,120 Um, here. 28 00:01:33,120 --> 00:01:42,930 So first we initialize the an integer array, uh, to the my array here with three elements, three, 29 00:01:42,930 --> 00:01:44,940 six and nine, the three elements. 30 00:01:45,030 --> 00:01:56,280 So next you initialize the integer pointer here to array Peter, which decays to a pointer. 31 00:01:56,400 --> 00:02:08,070 After initialization, the key array points to the first element of the my array here, which as you 32 00:02:08,070 --> 00:02:16,920 saw here, this when we print this just as an array, as a variable, um, we got a three the first 33 00:02:16,920 --> 00:02:18,510 element of my array. 34 00:02:19,170 --> 00:02:26,760 So we are now we're going to initialize an array containing colleague and we will pass the array to 35 00:02:26,760 --> 00:02:29,200 function as a pointer. 36 00:02:29,200 --> 00:02:33,580 So firstly, let's create a struct named colleague here. 37 00:02:36,470 --> 00:02:43,190 Or we instead of cooling, we can create the the car model names make model make names. 38 00:02:43,190 --> 00:02:52,810 So struct car cars here car and name will character name, character name. 39 00:02:52,820 --> 00:02:56,300 This is as you know strings is just a characters. 40 00:02:56,300 --> 00:03:05,480 So that's why we're going to we used here character name 2200 uh 56. 41 00:03:07,370 --> 00:03:11,390 So it can contain 256 characters in a row. 42 00:03:11,390 --> 00:03:21,170 So then we will use the create a printname function void Printname function here car. 43 00:03:22,350 --> 00:03:25,080 And car, Peter. 44 00:03:26,670 --> 00:03:34,350 Here printf s format specifier, car and new line. 45 00:03:35,160 --> 00:03:44,310 Then we will use a carpenter, but we will use the member of operator here name as you know as you can 46 00:03:44,310 --> 00:03:45,180 see here. 47 00:03:45,180 --> 00:03:47,760 So it's actually not print name. 48 00:03:47,760 --> 00:03:50,940 It's just a printf here. 49 00:03:53,360 --> 00:03:58,460 And then let's write our main function here. 50 00:03:58,460 --> 00:04:03,020 So car, my garage here. 51 00:04:04,080 --> 00:04:16,020 And we will add three car names, for example, BMW, Mercedes here, Mercedes and Ford. 52 00:04:17,020 --> 00:04:18,010 Fourth year. 53 00:04:19,090 --> 00:04:19,620 Fourth. 54 00:04:20,360 --> 00:04:22,040 Then we will print this. 55 00:04:22,040 --> 00:04:23,090 Print name. 56 00:04:23,990 --> 00:04:24,950 My garage. 57 00:04:32,650 --> 00:04:33,820 As you can see here. 58 00:04:34,640 --> 00:04:40,580 The print name function decays into a pointer when you call the print name. 59 00:04:40,940 --> 00:04:44,870 So because arrays decay into pointers to their first element. 60 00:04:45,770 --> 00:04:58,640 So my car here, my car printer at points to the first car in the my car names here, my garage here. 61 00:04:58,730 --> 00:05:03,360 So there's another array decay in the next lecture as well. 62 00:05:03,380 --> 00:05:06,290 So you use the arrow operator here. 63 00:05:07,280 --> 00:05:15,710 Here you use the arrow operator to access the name member of the car pointed to by a car. 64 00:05:15,710 --> 00:05:21,200 Peter So which itself is a character array, as we declared here. 65 00:05:21,200 --> 00:05:33,290 So the printf format specifier S expects a C style string, which is a character pointer and name decays 66 00:05:33,290 --> 00:05:36,820 into pointer to satisfy printf. 67 00:05:39,980 --> 00:05:44,270 And now we're going to handle the decay here. 68 00:05:44,390 --> 00:05:54,310 So often you pass arrays as two arguments, a pointer to the first array element and the arrays length. 69 00:05:54,320 --> 00:06:02,810 So that mechanism that enables this pattern is square braces, which work with the pointers just as 70 00:06:02,810 --> 00:06:03,830 with arrays. 71 00:06:04,010 --> 00:06:05,450 So now. 72 00:06:06,290 --> 00:06:12,890 Uh, we will employ this technique in and edit and reprogram this code here. 73 00:06:12,920 --> 00:06:17,960 So in print names we're going to add the for loop here. 74 00:06:18,900 --> 00:06:21,450 So here. 75 00:06:22,620 --> 00:06:23,130 Or. 76 00:06:24,260 --> 00:06:25,430 Size t. 77 00:06:25,460 --> 00:06:27,170 I will explain all of this code later. 78 00:06:27,200 --> 00:06:27,980 Don't worry. 79 00:06:28,280 --> 00:06:35,960 Size T and we're going to create a size T object named A and we will assign it to zero. 80 00:06:36,380 --> 00:06:42,370 And while E is less than, um, e is less than. 81 00:06:42,380 --> 00:06:45,530 Actually, we're going to take the another parameter here. 82 00:06:45,530 --> 00:06:47,960 Size t the. 83 00:06:49,380 --> 00:07:02,700 And cars while e is less than n cars and cars, then increment e by one. 84 00:07:03,620 --> 00:07:06,680 And here we're going to use the printer function. 85 00:07:06,680 --> 00:07:11,360 As you as you saw earlier, we use the member of operator. 86 00:07:11,360 --> 00:07:14,150 But this time we're going to use a different format here. 87 00:07:15,410 --> 00:07:20,720 So this will take Azure Strings, C-style strings here. 88 00:07:21,020 --> 00:07:28,220 We won't change that, but we will change this here because we will pass the another argument to this 89 00:07:28,220 --> 00:07:29,630 format specifier here. 90 00:07:30,710 --> 00:07:32,930 That is the. 91 00:07:37,410 --> 00:07:42,420 Actually, now we're going to use the just here. 92 00:07:44,010 --> 00:07:44,840 Cars. 93 00:07:45,330 --> 00:07:46,320 Cars. 94 00:07:46,800 --> 00:07:49,200 We will we change the parameter as well. 95 00:07:49,290 --> 00:07:53,730 So now we're going to use the cars here. 96 00:07:53,730 --> 00:07:58,140 We will pass E and then that your name? 97 00:08:00,030 --> 00:08:05,010 So and also we will here. 98 00:08:05,010 --> 00:08:07,080 As you can see, we won't change this. 99 00:08:07,770 --> 00:08:09,840 We have three. 100 00:08:11,170 --> 00:08:13,420 A strings in my garage. 101 00:08:13,450 --> 00:08:14,950 Which is. 102 00:08:15,850 --> 00:08:18,370 BMW, Mercedes and Ford. 103 00:08:19,630 --> 00:08:20,380 Then. 104 00:08:21,680 --> 00:08:23,960 We will use the print names function. 105 00:08:24,320 --> 00:08:24,950 Print name. 106 00:08:24,950 --> 00:08:26,780 We will call the print names function. 107 00:08:26,840 --> 00:08:32,900 And first, we're going to pass the my garage as an first parameter. 108 00:08:32,900 --> 00:08:34,220 And next. 109 00:08:35,410 --> 00:08:38,140 We will size of. 110 00:08:39,200 --> 00:08:39,680 Here. 111 00:08:40,950 --> 00:08:42,030 Size of. 112 00:08:54,130 --> 00:08:55,750 Size of my garage. 113 00:08:58,610 --> 00:09:02,120 Here divide by size of. 114 00:09:03,180 --> 00:09:03,740 Um. 115 00:09:06,770 --> 00:09:07,970 Size of car. 116 00:09:08,570 --> 00:09:08,990 Yeah. 117 00:09:09,560 --> 00:09:10,580 Size of car. 118 00:09:10,580 --> 00:09:12,500 As you can see here, this is a strict. 119 00:09:13,710 --> 00:09:18,570 So now we can execute our function here. 120 00:09:19,170 --> 00:09:19,830 Our code here. 121 00:09:19,830 --> 00:09:25,560 And as you can see here, we get a tree of our elements, array elements here. 122 00:09:25,680 --> 00:09:31,010 So now let's explain this code by line by line. 123 00:09:31,020 --> 00:09:37,050 So the print names function accepts an array in. 124 00:09:37,860 --> 00:09:41,400 Two arguments here, as you can see here. 125 00:09:42,270 --> 00:09:44,220 The first is. 126 00:09:47,030 --> 00:09:50,660 Uh, the first is the cars. 127 00:09:51,950 --> 00:09:56,960 And the second is number of elements in n cars here. 128 00:09:56,960 --> 00:10:05,300 So within the print names you iterate with the for loop and an index e here. 129 00:10:05,300 --> 00:10:10,160 So the value of E iterates from zero here. 130 00:10:10,190 --> 00:10:11,150 Zero. 131 00:10:11,780 --> 00:10:13,850 To, um. 132 00:10:14,120 --> 00:10:15,920 N colleagues. 133 00:10:16,580 --> 00:10:17,600 Minus one. 134 00:10:18,050 --> 00:10:28,310 So then you extract the corresponding colleague nine by accessing the, uh, here element here, and 135 00:10:28,310 --> 00:10:30,950 then get the name member. 136 00:10:31,760 --> 00:10:32,300 Here. 137 00:10:33,620 --> 00:10:37,830 The pointer plus size approach to passing arrays is ubiquitous. 138 00:10:37,850 --> 00:10:45,770 C style APIs, for example in Windows or Linux programming systems.