Welcome back.
In the last lecture, you learned that trucks can represent concepts by combining different types in
a single type in this lecture.
You're going to learn the basics of how to use trucks.
Let's first learn why you might want to use a struct.
Let's say you want to store data about two people.
I'm going to assign some data to the variables.
Let me print them first, because then I'm going to print fraud.
These variables have no connection between them, so they don't act as a single unit because they don't
represent a concept name.
Last name and age are different concepts on their own.
You can group these variables in a single unit using a struct so you can more correctly represent the
person concept.
First, I'm going to copy the multiple declarations from here.
I'm going to paste it here.
I'm going to say struct next I'm going to replace the parentheses with the curly braces.
Lastly, I'm going to name the variable as Picasso.
That's it.
By the way, I could also have typed this like, so it's the same thing here, the Picasso is a strike
value and it contains all the variables.
Picasso is the name and this part is the type of the variable name, last name and age are now called
the fields of the Picasso struct.
These fields are still variables, but now they are part of the Picasso's tract.
So you can only access them through the Picasso variable.
By the way, each field in a struct should be unique.
For example, you cannot add another field like so because there is already an age field in this tract.
Them also create another struct for Sigmund Freud Lexar.
These two variables have the same type because their fields are the same.
It's because their fields are in the same order and they have the same field names and field types.
Now, let's talk about how the print extract value and let's see what does a struct zero value look
like.
I'm going to use the Vivir with the plus modifiers to print destructs.
This will print them along with their fields.
OK, let's see what is going to print it.
For instance, tracks with their field names and field values.
I was providing name, last name and age variables to the print have previously.
However, here I only pass the values.
This is the most important knowledge that you need to remember about strikes struck values act as a
single value, as a single unit.
That is why they are so powerful.
Right now, all the fields are empty because I didn't initialize them yet, so each field gets a zero
value depending on its type.
You know what happens when you don't initialize a variable?
It gets a zero value, right?
It is the same here because each field of struct is a variable.
I'm going to show you how to set them in a minute.
For now, let's leave them as they are with zero values.
These are called anonymous tracks because they don't have a name.
That's why I needed to duplicate the same struct type twice in here.
But it looks very verbose and confusing.
Right.
So how can you improve this while you can define a new struct type?
Only wants to do that.
I'm going to declare a new structure, LACHSA, and I'm going to create a new type and I'm going to
give it a name person LACHSA.
So now you got a new structure.
Now it's time to use it in our code.
For now, I'm going to replace old anonymous tracks with the new person type LACHSA.
That's it.
Much better, isn't it.
It should work as exactly as before.
Let's try it while it works.
Now it's time to talk about how to retrieve and change the field values.
Let's start with Picasso first.
I'm going to set his name to do that.
First, I need to use the Picasso variable and I'm going to type that.
This selector operator selects a field inside the struct value.
I can list all the fields of the struct by pressing control space like.
So first I'm going to set his name.
Now let's get his last name and his age.
I can change the fields because they are variables.
It works, the fields don't contain zero values.
Now, there's also change the fields of the fraught.
Here are the name, last name and age of fraud in a single strike value, by the way, as I said, you
can retrieve the individual fields as well.
For example, I'm going to print only the last name and age feels like so.
Now, we don't know the prince, the last name and age feels.
Of course, I cannot retrieve a field that doesn't exist.
Now, let me show you another printf word modifier here, I'm going to change all the plus modifiers
to the sharp modifiers LACHSA.
Now, it also prints the types of destruct values and it produces a text that I can use in the code
directly.
Now let me show you, how can you initialize the fields using a literal.
For example, I'm going to copy the output of Picasso from here.
Now I'm going to use it for initializing the A variable like so.
Let me also remove the assignments.
I don't need them if this truck were in another package, I would need to select it using the package
name.
But here I'm already in the main package, so I don't need to use it.
As you can see, the program still works, so this is what a struggle it looks like.
It's a composite Litoral that you are already familiar with.
Here is its type and here is its value.
I can also type it like saw the difference between this Litoral and Assignment's is the -- about
initializes the strike value right from the beginning, but the code blow-by-blow only changes an existing
Stracke value.
By the way, you use the field names, you don't have to provide all the fields, let's say, I don't
know the age of Picasso.
So the edge field gets a zero value automatically.
For example, now I'm going to remove the field names.
As you can see, go automatically initializes done for you by matching the ordering of the fields with
the values, for example, let me move the edge here.
As you can see, there is an error here, I try to assign nine to one to the name Stringfield, so there
is a type mismatch.
Now also here, I try to assign Pablo to the field, which is an infield.
So why don't use the field names?
The field order becomes important.
However, if you use the field names like so, the order doesn't matter.
Now it initializes the correct feels, even though their order is different than the struct type itself.
If you own the strike type and if it's a simple strike like this one here, you can initialize a struct
without using the field names.
However, if the strike type were in another package that you didn't own, it's probably better to use
the field names because the owner of the package could change the field orders in the future and doing
so would break your code.
The best practice is using the field names.
All right, that's all for now.
See in the next picture by.