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, what is going on, ladies and gentlemen?
Let me see that everything works as expected.
Yes.
So in this video, what we are going to do is to proceed with our tutorial and with our learning process
of unions in C programming language.
OK, so what we are going to do in this video is simply to see additional usage, additional structures
or additional options of creating and working with unions.
Maybe we will talk about type definition.
We will talk about or maybe a pointer on a union type.
And also we may also talk about a raise and let's see basically what this lecture will lead us to.
So get yourself ready, get some something nice to drink and prepare yourself, because there we go.
Let's do it.
All right.
So we are talking about unions, unions.
And what I want us to do now is basically let's make a quick reminder of how we can define a union.
OK, so basically saying let's create a union of a student.
OK, so union student and let's use a couple of fields.
What fields should we use?
Let's use I don't know, let's use some I.D. and also let's use some GPA.
OK, so two fields to values under this union student.
And basically saying, what what do you think, what does it mean?
OK, these two fields, it's going to make it more interesting and use here.
I don't know, let's use double jeopardy.
OK, so basically, you know what it is basically stands for a rating in education.
OK, so that's just some sort of an average for those of you guys who is not familiar with it.
So maybe for all of you guys, because you're coming from a lot of places, so double average.
So to feel it and double average.
So that's our union.
OK.
And basically we already know how we can create a variable of these type, so let's use it like this.
So union student and let's make student one variable.
So student one OK, and now we know that we can access and change any of its fields.
But what do you think guys are actually happens behind the scenes?
What do you think will be created now?
So printout signs of student one union equals two, let's say percentage.
And here use instead of these percentages, we will use size of size of student one.
OK, so what do you think should be printed to the screen?
Do you think a value of four should be printed?
Do you think a value of eight should be printed?
Do you think a value of four plus eight should be printed since we created this student one variable
which is of type of union student?
What do you think?
So take a second, think about it and I'm going simply to execute it right away.
Basically, you can see that the size of student one union equals to eight.
And basically why why is that?
Why is this happening?
And the reason for that is very simple.
We already talked about it.
Let's just simply make a brief.
So if we created student one, OK, so that's student one.
That's the variable.
STUDENT one And here it will have not both of these fields if it was like considering both of the fields.
So it would be like four bytes plus eight.
It would be a total of 12 bytes.
But we simply get the memory for the largest element between these fields.
And in this case, that's the double type and it consumes eight bytes of memory.
So aid bytes of memory in this region in memory will be used to store either the I.D. or it will use
be used to store the average.
So one of them will be stored here.
OK, that's in contrast to how the struct work, which basically you would have got here like two different
and separate fields, ID and Everet, but that's something we already talked about in the previous menials.
I just wanted to show you here how how this size of operator will work and how it will look like on
this union.
Awesome.
So now we proceed and also talk about how we can create a pointer of a union type, so of course we
can create a pointer because we just made this user defined new type and we can create a pointer just
by using unión student and here specified, I don't know, PDR student two.
OK, and we will say that BTR student two OK, is just a pointer.
OK, what do you think will be the size.
What do you think will be the size.
What do you think will be the size of our PDR student too.
OK.
What do you think will be the size.
Will it be eight.
Again, just like the previous one, or basically will it be of a different value?
OK, what do you think you should by this point, you should basically know the answer to it.
And the answer is very straightforward because you know that every pointer is just the variable that
should hold that should hold the address of where the actual variable with the actual value resides.
And this type of pointer simply holds the address.
And this address is specified by four bytes of memory.
So it has nothing to do with these union type.
It's basically a variable called PDR student to use.
This variable is used to store the address of some union that we currently even do not point to.
So I hope that's clear to you.
And know, what we are going to do is basically to make some operations.
We are going to say PDR student, OK, dot average or let's say it equals to five.
OK, and we are going also now to print the value.
OK, so print printout student one dot idee equals two percentage.
Do we know that it can be printed out like this.
OK.
It can be printed out like this, so let's build and run it to make sure that everything works as expected.
What's going on here?
Oh, sorry about that.
It's probably the problem.
So, Keoki, something seems to not to be working as expected.
Oh, so you will also have to fix it like here.
So that's sort my my bad my bet I am that another down the road.
Didn't that them see in what's going on.
Um, oh, basically the problem is here, so we simply we simply haven't specified where the BTR students
should point to.
So you have done here a couple of mistakes.
I'm sorry for that.
Let's just fix that out, OK?
So let's let's let's start from the scratch, because I think that that's not the best way to go.
So meeting our students, you should have the address of student one, OK?
And that's the way we use pointers.
So now PDR student two points.
OK, so it points to student one variable.
Student one variable.
Awesome.
You know, let's use BTR student to basically not let's first of all, use just our student, one student
one just to demonstrate what happens here.
Student one equals two, what it should be equal to, let's say five.
OK, and now print this value right here.
OK, so let me get it straight.
OK, here also.
Now let's do it like this.
OK, so now let's print student one I.D. just to show you that everything is working OK before.
OK, that's before we are using the pointers.
So let's build and run it and there you go.
You can see that student one Darity equals two five.
OK, awesome.
Now let's try to change the side by using these BTR student two pointer.
So your student, your student, let's say I d was to 210.
Let's once again use this line to see what will happen after we changed it using the the pointer of
PDR student two.
So there you go.
You can see that behind the scenes, BTR are student two has changed the value in the idea of student
one already.
So if we want to take a look at how it basically looks like behind the scenes.
OK, so let's just remove this one, OK?
So we created one variable called student one.
Student one.
And it's of tight union type of union.
Union student, that's something we know so far.
And we also created these PDR student, too, which is a variable of a pointer to union student type.
OK, so that's PJR student to.
And it basically points to these to these variable, and by using this pointer, we can simply also
change its values.
OK, so that's nothing new about pointers.
That's probably something you already know by now.
But just I wanted to demonstrate all of these all of this concept.
Awesome.
You know, last thing that I want to maybe last, maybe not to discussing this video is also that you
can use here, like you can use typedef type definition unions, student and I don't know, let's call
it Euromed, you know, instead of writing down like every time a union student and so on and so forth,
you can simply use student, student and then specify student one or a student too.
And these will basically create a union student user defined data type.
And then you can access simply you remember how we used these typedef in our structures.
So pretty much the same here.
OK, awesome.
Now, what also what else can we use here?
What else can we use here?
Let's try maybe to use oh, let's try to create an array of students, OK?
Students, students are ready.
OK, let's make it a size three and then we'll see that students are let's use and look.
So in time let's go like this.
So for all equals to zero.
As long as I is less than three I plus plus at least case all you have to do is just to use to print
out answer I.D. one.
Then he is caniff percentage, the store, this value inside of students array at Index I and specify
also dot d for example, I'm just showing you a few examples of how look what looks like a behind the
scenes.
So for angles now let's pretend I did not want a D let's say percentage D OK here specify I gave.
So enter ID number see.
Number one.
Number two.
Number three.
Awesome.
So printout here we will see that student.
Student number percentage D, I guess it was two percentage dealing here with specified also, what
should we specify?
I plus one for these first I.D. number and for for these percentage which which numbers student reviews
and also ID equals the percentage and that should be basically students array index.
I got a D..
OK, so basically ah let's just check it out that everything works as expected.
So inter I.D. number one, let's say five.
Let's see, we are let's say 10 basically.
There you go.
Student number one ID equals to five.
Student number two, it equals to three in student number three equals to.
So basically you can see that you created an array of unions the way you created it this way.
That's an array of unions.
And what you've done here is basically two steps, three steps.
First step was to create and define basically the union itself, union student.
Then we used these type of definition to create these student nickname.
And finally, the first step was like to create an array of students.
And you simply specified the array name, the array size, and then you used some loop to you read inputs
from the user and then you printed out all of these information.
OK, so that's basically.
A lot of our usages cover I cover tried to cover at least half the most useful information that you
may actually be using on your exercises or basically on your exams or when your development using unions
in our C programming language.
So I hope everything is clear so far.
And yeah, I guess this is it for these video guys.
I hope this information will come in handy too.
It will come handy to you.
And until next time, keep practicing, keep them moving forward and we will see each other in the next
videos.
Until then, bye bye.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.