WEBVTT 00:01.490 --> 00:02.300 Welcome back. 00:02.750 --> 00:09.050 In the last lecture, you learned that trucks can represent concepts by combining different types in 00:09.050 --> 00:11.210 a single type in this lecture. 00:11.330 --> 00:14.300 You're going to learn the basics of how to use trucks. 00:14.810 --> 00:17.840 Let's first learn why you might want to use a struct. 00:18.320 --> 00:21.760 Let's say you want to store data about two people. 00:22.070 --> 00:24.540 I'm going to assign some data to the variables. 00:25.190 --> 00:29.330 Let me print them first, because then I'm going to print fraud. 00:30.020 --> 00:36.530 These variables have no connection between them, so they don't act as a single unit because they don't 00:36.530 --> 00:38.810 represent a concept name. 00:38.810 --> 00:42.350 Last name and age are different concepts on their own. 00:42.650 --> 00:48.710 You can group these variables in a single unit using a struct so you can more correctly represent the 00:48.710 --> 00:49.940 person concept. 00:49.950 --> 00:53.240 First, I'm going to copy the multiple declarations from here. 00:53.450 --> 00:54.770 I'm going to paste it here. 00:55.470 --> 01:00.840 I'm going to say struct next I'm going to replace the parentheses with the curly braces. 01:01.370 --> 01:04.570 Lastly, I'm going to name the variable as Picasso. 01:05.000 --> 01:05.570 That's it. 01:06.610 --> 01:13.420 By the way, I could also have typed this like, so it's the same thing here, the Picasso is a strike 01:13.420 --> 01:16.150 value and it contains all the variables. 01:16.160 --> 01:22.660 Picasso is the name and this part is the type of the variable name, last name and age are now called 01:22.660 --> 01:24.520 the fields of the Picasso struct. 01:24.910 --> 01:29.500 These fields are still variables, but now they are part of the Picasso's tract. 01:29.890 --> 01:33.120 So you can only access them through the Picasso variable. 01:33.790 --> 01:36.760 By the way, each field in a struct should be unique. 01:36.770 --> 01:42.250 For example, you cannot add another field like so because there is already an age field in this tract. 01:42.910 --> 01:46.180 Them also create another struct for Sigmund Freud Lexar. 01:47.090 --> 01:51.570 These two variables have the same type because their fields are the same. 01:52.220 --> 01:58.100 It's because their fields are in the same order and they have the same field names and field types. 01:58.430 --> 02:04.430 Now, let's talk about how the print extract value and let's see what does a struct zero value look 02:04.430 --> 02:04.700 like. 02:04.850 --> 02:08.930 I'm going to use the Vivir with the plus modifiers to print destructs. 02:09.170 --> 02:11.660 This will print them along with their fields. 02:12.020 --> 02:14.800 OK, let's see what is going to print it. 02:14.810 --> 02:17.960 For instance, tracks with their field names and field values. 02:18.440 --> 02:22.780 I was providing name, last name and age variables to the print have previously. 02:23.240 --> 02:26.430 However, here I only pass the values. 02:26.750 --> 02:33.350 This is the most important knowledge that you need to remember about strikes struck values act as a 02:33.350 --> 02:35.780 single value, as a single unit. 02:35.990 --> 02:37.820 That is why they are so powerful. 02:38.850 --> 02:44.940 Right now, all the fields are empty because I didn't initialize them yet, so each field gets a zero 02:44.940 --> 02:47.130 value depending on its type. 02:47.340 --> 02:50.310 You know what happens when you don't initialize a variable? 02:50.700 --> 02:52.540 It gets a zero value, right? 02:52.770 --> 02:56.420 It is the same here because each field of struct is a variable. 02:56.730 --> 02:59.470 I'm going to show you how to set them in a minute. 02:59.490 --> 03:02.710 For now, let's leave them as they are with zero values. 03:03.180 --> 03:06.790 These are called anonymous tracks because they don't have a name. 03:07.050 --> 03:11.040 That's why I needed to duplicate the same struct type twice in here. 03:11.130 --> 03:13.800 But it looks very verbose and confusing. 03:13.800 --> 03:14.140 Right. 03:14.400 --> 03:18.870 So how can you improve this while you can define a new struct type? 03:18.870 --> 03:20.390 Only wants to do that. 03:20.400 --> 03:25.920 I'm going to declare a new structure, LACHSA, and I'm going to create a new type and I'm going to 03:25.920 --> 03:28.030 give it a name person LACHSA. 03:28.230 --> 03:30.250 So now you got a new structure. 03:30.450 --> 03:32.580 Now it's time to use it in our code. 03:33.060 --> 03:38.060 For now, I'm going to replace old anonymous tracks with the new person type LACHSA. 03:38.250 --> 03:38.790 That's it. 03:38.970 --> 03:39.990 Much better, isn't it. 03:40.290 --> 03:42.660 It should work as exactly as before. 03:42.720 --> 03:45.070 Let's try it while it works. 03:45.540 --> 03:50.370 Now it's time to talk about how to retrieve and change the field values. 03:50.790 --> 03:53.220 Let's start with Picasso first. 03:53.230 --> 03:55.320 I'm going to set his name to do that. 03:55.320 --> 03:59.430 First, I need to use the Picasso variable and I'm going to type that. 03:59.610 --> 04:03.570 This selector operator selects a field inside the struct value. 04:03.750 --> 04:08.100 I can list all the fields of the struct by pressing control space like. 04:08.110 --> 04:10.440 So first I'm going to set his name. 04:11.460 --> 04:14.210 Now let's get his last name and his age. 04:16.650 --> 04:19.460 I can change the fields because they are variables. 04:20.740 --> 04:23.930 It works, the fields don't contain zero values. 04:23.960 --> 04:26.860 Now, there's also change the fields of the fraught. 04:31.300 --> 04:37.600 Here are the name, last name and age of fraud in a single strike value, by the way, as I said, you 04:37.600 --> 04:40.010 can retrieve the individual fields as well. 04:40.540 --> 04:44.770 For example, I'm going to print only the last name and age feels like so. 04:49.890 --> 04:52.500 Now, we don't know the prince, the last name and age feels. 04:53.830 --> 04:57.550 Of course, I cannot retrieve a field that doesn't exist. 04:59.430 --> 05:05.220 Now, let me show you another printf word modifier here, I'm going to change all the plus modifiers 05:05.220 --> 05:07.670 to the sharp modifiers LACHSA. 05:08.870 --> 05:15.110 Now, it also prints the types of destruct values and it produces a text that I can use in the code 05:15.110 --> 05:15.620 directly. 05:15.890 --> 05:19.850 Now let me show you, how can you initialize the fields using a literal. 05:19.880 --> 05:23.480 For example, I'm going to copy the output of Picasso from here. 05:24.740 --> 05:28.190 Now I'm going to use it for initializing the A variable like so. 05:29.150 --> 05:31.340 Let me also remove the assignments. 05:31.370 --> 05:37.700 I don't need them if this truck were in another package, I would need to select it using the package 05:37.710 --> 05:37.970 name. 05:38.300 --> 05:41.870 But here I'm already in the main package, so I don't need to use it. 05:44.130 --> 05:48.720 As you can see, the program still works, so this is what a struggle it looks like. 05:48.880 --> 05:52.040 It's a composite Litoral that you are already familiar with. 05:52.290 --> 05:54.780 Here is its type and here is its value. 05:55.320 --> 06:02.460 I can also type it like saw the difference between this Litoral and Assignment's is the -- about 06:02.460 --> 06:09.360 initializes the strike value right from the beginning, but the code blow-by-blow only changes an existing 06:09.360 --> 06:10.230 Stracke value. 06:11.200 --> 06:17.710 By the way, you use the field names, you don't have to provide all the fields, let's say, I don't 06:17.710 --> 06:19.150 know the age of Picasso. 06:20.060 --> 06:23.240 So the edge field gets a zero value automatically. 06:24.320 --> 06:26.750 For example, now I'm going to remove the field names. 06:27.780 --> 06:32.970 As you can see, go automatically initializes done for you by matching the ordering of the fields with 06:32.970 --> 06:36.960 the values, for example, let me move the edge here. 06:38.040 --> 06:44.220 As you can see, there is an error here, I try to assign nine to one to the name Stringfield, so there 06:44.220 --> 06:45.090 is a type mismatch. 06:45.090 --> 06:50.400 Now also here, I try to assign Pablo to the field, which is an infield. 06:50.850 --> 06:52.830 So why don't use the field names? 06:53.010 --> 06:55.470 The field order becomes important. 06:55.750 --> 07:00.120 However, if you use the field names like so, the order doesn't matter. 07:02.360 --> 07:08.980 Now it initializes the correct feels, even though their order is different than the struct type itself. 07:09.950 --> 07:16.490 If you own the strike type and if it's a simple strike like this one here, you can initialize a struct 07:16.490 --> 07:18.110 without using the field names. 07:18.490 --> 07:24.590 However, if the strike type were in another package that you didn't own, it's probably better to use 07:24.590 --> 07:30.260 the field names because the owner of the package could change the field orders in the future and doing 07:30.260 --> 07:31.730 so would break your code. 07:31.970 --> 07:35.060 The best practice is using the field names. 07:36.070 --> 07:37.760 All right, that's all for now. 07:37.990 --> 07:39.250 See in the next picture by.