Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, so now let's talk about how this union variable basically looks like behind the scenes.
So, first of all, simply saying when you created this variable, these my variable one of type union
info, then this union definition basically seems like to have a few fields.
Right.
There is the first name in the age, but this does not mean you created a structure.
So if it was like I don't know if it was like, let's say Trachte.
OK, so I don't know, tracting info.
And in this case there were like also two fields.
OK, so field one in field two.
And basically then when you would have created the struct, it will look like this.
So there was like a separate place for each field.
So this was a place in memory for field one and a place in memory for field two.
But that's basically not what happens for unions.
For unions, there will be like one place, OK, in memory.
And this place is going to be of the largest of the size of the largest element in the union structure,
in the union definition.
In this case, we know that these int age are acquirer's like four bytes of memory and these characters
are Raees of twenty bytes.
So what we can say that the the memory that will be spent for these variables.
So my variable one, it will not be like 20 bytes plus four bytes, which was like something for the
structure of the world, like 20 bytes for the first name array of characters and four bytes for the
age.
But rather since that's a union, we are going to have only for the largest field.
In this case, it's just 20 bytes of memory.
OK, and from this place, from this memory region, we are going basically to be able to store information
regarding first name or the age so either of them can be stored here.
But you see, whenever you're going to write some information, let's say for the first name inside
of this region in memory, then the value for the age may also get changed.
And let me show you, I think that better example to demonstrate exactly what I mean.
So let's go and do something like this.
Well, let's say my variable, my variable, one dot age equals two thirty.
Now let's use, I don't know, some print line.
OK, so let's use let's use print F to F and print my variable.
One dot age equals two percentage D basically see what it will print us.
So my variable one dark age and let's build and run this program and see what happens.
So basically, there you go, you can see what is printed on the screen.
Let me just remove all of these drawings so you can see my variable one Dorridge equals to 30.
So everything as expected so far.
OK, but if we are going to do something like this, let's see my variable one, my variable one dot
first name for his name.
Let's say it will be equal to Hello String.
OK, so there will be like information regarding these first name array of characters.
So we are going to put there, I don't know, something like these little nice string.
And I'll let's try to build and run it and see what happens.
OK, so basically.
Oh of course.
Of course.
Of course we cannot do it this way.
So let's just fix it up quickly.
Let's just include the string that h liberatory.
So string dot age and basically use here.
Let's use here is the CPI if you want.
Yeah.
If I'm not mistaken it's the RCP y and passing here two values.
So you can see the, the signature and the structure basically of these SDR supervisor copying one thing
and from copying these string into this region in the memory.
So let's try to build and run it once again, a semicolon at the end.
So building Iranians, OK, so now everything seems to be working correctly.
But now what I want us to do is simply let's try to print this string to the screen.
OK, so let's see this.
I don't know, put us and put this string to the screen.
So let's say let's better say print out my variable dot first name equals two percentage s and basically
simply saying, oh, my variable dot first thing first.
OK, what do you expect to see now on the screen?
Do you expect to see hello.
Do you expect to see something else.
So let's build and run it and see what happens.
So it seems like everything is working correctly and it's very similar to how this products work in
C programming language.
So there is like my variable one age equal to 30, my variable.
That first name equals to hello.
So it seems OK, it seems like are from first glance it seems like there are basically two fields,
one for the age and one for the first name.
And basically here we have like a value of thirty and here we have like a string of hello.
So everything seems to be fine, but if that's the case, if that would be the structure, then it would
make sense that if I'm trying if I would try to print this value once again, if I would try to print
Vade once again, then basically the value of 30 will also be printed.
Right.
So let's build it and run it and see what happens.
So there you go.
That's the new execution of this program.
So beforehand, my variable, one age was equal to 30 and then reprinted below because we use the recipe.
Why was simply assigned the string hello to the my variable one doctor's name.
And then we printed it out to the screen and then we did not change it.
Any step in this program, my variable one that age once again.
But the value printed here does not make any any sense at all.
So why was it changed?
Why was this my variable at H equal to 30 now changed to this strange value.
And the answer is very simple guys.
That's because we used unions and not a structure where in the structure that there was a specific separated
block for of memory for each of these fields and for unions, we simply have one region in memory that
this region will store information for either of them.
OK, so there is this age.
First of all, you put here 30 and then you said there was also a first name.
And if I want to change its value.
OK, so I simply remove and remove it and overrated these value of thirty.
And I put it here like the string.
Hello.
OK, so then again, when you tried to like to access using this field of age in this union info variable
that you created my variable one, then it's going to try to see the information.
But the information regarding these 30 does not longer exist there.
So it's like use this percentage D and kind of grabs some information from this L and so on.
OK, so that's basically what happens behind the scenes when you are using union variables.
All right.
So I hope that's clear.
And now just to emphasize and show you the differences, OK, show you the differences between struct
and unions.
OK, so let's first of all, see just are these Samori, that first thing a struct is just a block of
memory, which stores alike like elements and fields that do not overlap while the union is also a block
of memory.
OK, but it's stores, these several fields on just one place.
OK, so they store it like taking the largest of these fields.
And that's the only memory that is being allocated for these union variable.
And that's why you can only store one of the data fields at any point in time when you were using Unions'.
So let's see additional example.
Maybe it will be better to demonstrate how it looks like.
So what's that?
OK, so let's create struct.
Let's create, let's create point struct.
OK, so point struct in this structure is going to have two fields and X and Y.
OK, and also we are going to create a union point union in this union is also going to have like two
fields named X and Y.
OK, and now the main function, let us let us create create two things.
Let us first of all create a structure.
OK, so let's create struct Trachte points struct.
Let's call it point one.
OK, that's the variable name.
And also let's create a union union struct union or not union.
What was that point.
Union point to union point.
Where is it, union formed union.
OK, so union point union, let's go and point to.
OK, so two very Jarrell's we created point one and point two.
OK, now let us say the following thing.
Let us say that points from point one dot x will be equal to five and point one that Y will be equal
to seven.
OK, so no problem about it.
We can simply print like let's say plaintiffs are Throgs Point equals to like let's say percentage and
percentage B..
So here will be the X value in here will be the volume value.
OK, so point one dot X and point one that Y.
OK, so that's basically about FrontPoint.
And if we will try to like to build and run it.
Yeah, let's build and run it so you can see strongpoint equals two, five and seven, nothing special.
And now what I want us to do is also to set up some information regarding regarding let's do it here
regarding union.
So point to point to that, X equals to let's say three.
OK, and that was I'm going to print if I'm going to let's see if I'm going to print our union point
equals to percentage and percentage point two and point two.
OK, so I did not actually initialize any value for the Y field in this union.
Right.
Basically I've done it for the struct, but I haven't done it for the union.
So if I'm going to build and run it.
What do you expect will happen?
So basically, if we will simply we're not using point one dot y equals to seven, then in this case
for a struct, there will be like strange value, I don't know, 50 somehow got here, 50.
I don't know where from this value even got here.
So basically if we will build and run it, you will see that now struct point equals to five and seven
and the union point equals two, three and three.
So why do you need initialize in whity to assign a value of three also to the Y?
So that's again the explanation for how you use basically the the difference between struct and unions.
So here is a struct.
OK, that's the struct for variable point one.
OK, so that's point one.
And it has two fields X which equals two five and Y which goes to seven.
And from the other hand.
OK, so first of all, here are four bytes of memory and also here are four bytes of memory, but four
point two, which is of a union type.
There is no special dedicated field for X and Y.
OK, there is no special field.
There is only four bytes of memory, OK, for bytes of memory since the largest element, the largest
field here is a four bytes of memory.
And inside of this area, we simply store a value.
So when we said point to dot X equals the three.
So you go to this region in memory and you set up here three and you can access these three from this
X or from this Y.
That means that if we are going to change to change the value of X or Y, let's say point to dot Y equals
two, let's say four, then in this case you will see that also the X value will be change will be changed
since that's the same region in memory.
So let's say after a change union point equals to this value.
So let's see what happens.
So there you go.
Struct point five seven three three.
And then you changed after change.
Union point equals to four point four.
OK, is that clear?
So that means that here in this region we would have like the value of four guys.
Awesome.
So but what will happen basically about the struct.
OK, so let's say we also would have changed like point one Y equals to ten.
Will it also be the same for struct.
Will also both of its values will be changed.
What do you think guys.
Let me know to take a second.
Think about it.
And let's say after a change struct point equals two point one point one does point.
Wonder why.
So what do you think.
What do you think?
So that's basically all is basically build and run it.
And there you go, you can see that only the value of point one dot y has been changed.
OK, you can see that because they are totally separated or regions in memory and they do not affect
one another.
So that's a good thing to know.
That's basically mainly the difference between unions and struct.
So I hope this quick introduction, weak explanation is clear to you guys, and if you still have any
questions or if you want me to make, I don't know, some more descriptive summary perhaps about our
own advantages and disadvantages of using one thing over the other.
So please let me know and hopefully we will see each other again in additional videos.
So until then, have a great time and I wish you the best of luck.
Bye bye.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.