Application of Unions (Part 1)
HTML-код
- Опубликовано: 5 окт 2019
- C Programming: Application of Unions in C Programming.
Topics discussed:
1) Accessing the members of the structure.
2) Printing the size of the structure.
C Programming Lectures: goo.gl/7Eh2SS
Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
Contribute: www.nesoacademy.org/donate
Memberships: bit.ly/2U7YSPI
Books: www.nesoacademy.org/recommende...
Website ► www.nesoacademy.org/
Forum ► forum.nesoacademy.org/
Facebook ► goo.gl/Nt0PmB
Twitter ► / nesoacademy
Music:
Axol x Alex Skrindo - You [NCS Release]
#CProgrammingByNeso #CProgramming #UnionsInC #UnionApplications #Structure&Union
Sir.. Missing ur lectures.. .. Huge fan of ur teaching
Sir when us data structures is going to start
how to access price of an item in above structure... s.item.price=9? or s.price=9? explain
@Neso Academy
s.price
Thank u sir u cleared my all doubt related to union
Awesome tutorial, you go to the point and explain clearly what unions do which is what i wanted to know. Thaks!
Wonderful... Thank you so much.. Happy दशहरा ..
Ur classes are just awesome!
Very clearly explained....thanks
Excellent to the point explanation.
Here is a big fan from Turkey, thanks for your videos
Very helpful!
nice one presentation sir i love this too much because your explanation is awesome
great example here! :) Thanks!
This is great. Thank you so much 🙂
The alchemist is a great book, Sir! claps from Brazil
Neso>>>>every other utube lecturer 🎉
loved it sir ji
Thank u very much for the video. Sir please also make video on union inside structure and structure inside union...
thank you very much sir for this video
Finally NESO Academy listened !!
thank you
Sir please keep uploading regularly.Its kind of a habit now
Thank you
Love from Antartica ❤❤❤❤
but if they share the same memory location modifying the values of book swill affect the values of shirt. right ?
Two doubts:
1. In the example mentioned , how do you differentiate between book's price and shirt's price? In other words, why didn't you place the 'price' member within structures inside union?
2. In union, members will share the same memory location. That means, if we make changes to one member, it will be reflected on the other member as well, isn't it? How come it is not happening here?
Is creating a separate variable for book is a solution for doubt 1?
struct store b;
Answering to your 2nd doubt,
Yes it will be reflected in the other member of the union.
And that's why sir told to use either book structure or t shirt structure. We can't use both.
Luv it
Wouldn't it be helpfull to define an additional variable in struct store to distinguish between book and shirt? Otherwise you don't know, what kind of information will be found within the union.
In the video for code at 4:35, I could not understand why there was no name after union or struct ; why directly { started. Could you please help me out with this one? Thank you.
In the main function there is no use of color ,size and design then why there is need of enter structure varible of these just for the purpose of memory saving????
Nice book tastes :)
huge fan sir please make an leacture fo python😀😀😀😀🙂🙂🙂
best book alchemist
Does union also have the concept of padding?
How could I initialize the price of the book/shirt and access it?
You said union members share same memory location and change in one gets reflected in others members too. Then "book" and "shirt" structures are two members of the Union. If we set values in book members then why is it not reflected in shirt members?
it reflected in my code tho. I din change the data type in printf() and showed same result with 0 errors.
I put the same data type as book just changed the name book to "shirt"
like::
printf("%s", s.item.book.title); and printf("%s", s.item.shirt.color); showed the same output with 0 errors.
Even though s.item.book.title = string literal and s.item.shirt.color = int data type
When I used both with "%s" they showed the same output. I don't know why.
I had the same question as yours.
@@badxcode same here..!!
Sir if we add all the members to a single union what is the problem. Then the total size will be 8 byte.
Which situation we use union inside structure
Sir plz upload data structures in c programming
Can anyone please explain why the struct declared under union has no name associated with it. Was it a mistake???
What does the # pragma pack(1) mean
how to access price of an item in above structure... s.item.price=9? or s.price=9? explain
@Neso Academy
Great video.
And why don't we just separate store to two structs like struct book and struct shirt?
because when creating two different structs and you include the member "price" in both structs waste space.
y we using pointer to specific member title author and design
There's a question ..how will you differentiate between the price of book and the price of shirt?
s.item.?.price= x
So in place of ? Whatever u insert is the price
Upload network lectures sir
Naming of structures book and shirt are missing in the last application 5:20
how to add price ??
Why the character are declared as pointer but not array?
if we remove structs inside unions and include 2 unions, we get size as 16
why char ptr is holding 8 bytes??
How first structure has size 20 bytes
For my 64 bit processor pointer (char *, int * , double * ) all taking 4 bytes
What if I want to store the information of book and shirt at the same time sir??
Don't use a union
@@penfever pakka?
What if I want to also store a shirt information and want to display both book information and shirt information at the same time ? Can I do it ?
yes you can do that
bro i think you are not clear with how code is executed
pointe size is 8 bytes but title takes 12 bytes of memory how it is managed
Something is wrong with the code, i cannot run it! Pls someone guide me
fell in love with your channel, but why always the char variable has to be pointers? why every char variable has the * before it?!
As we have to store string value like author name , we need need to declare char pointer which represents first memory location of our string char
it is called "String literal" you can watch neso vedios, it is simply a way to store strings
how char is taking 8 bytes instead of 1 bytes
How can COLOR be an integer type?
Just consdering for your understanding on concept of unions
Just consdering for your understanding of unions
it seems to me that this program not perfect and has lots of problems
personally I had problems in understanding it :(
I could not add two books or shirts!
how could I create variable inside main not in the structure definition.
But my compiler showing the size 32bytes.
Sir if we use the shirt properties
Then what will be the size of union??
The size of the union will *always* be that of its largest member, even if you use the smaller member. If we, as in your question, use the shirt, it will still use the size of the book (but wont use it all).
@@lionkor98 thnxxx 🙏🙏🙏
@@lionkor98 if we, assign values to the variables of t shirt structure, then won't the values of the variables of book structure get affected? Because these structures are stored inside union right.....so they have same memory allocation and varying one member affects the other.
Am I correct?
Why is this code giving error in C++
C or C++?
sir is that possible to use union 3:17 here
instead of struct
//like this
#include
union store{
double price;
char *title;
char *author;
int num_page;
int color;
int size;
char *design;
};
int main()
{
union store book;
printf("%ld bytes
",sizeof(book));
return 0;
}
how color can be int, and why every character is a pointer
explain anyone
pleaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Only curious person search this topic
char * takes 8 byte i don't understand please help me
any pointer pointing to astring literal takes 8 bytes of memory
// WE GET SIZE AS 16, IF WE remove structs inside unions and include 2 unions
#include
#include
struct store{
union{
char *title;
char *author;
int pages;
}book;
union{
char *color;
int size;
char *design;
}shirt;
};
int main(){
struct store s;
s.book.title = "Naruto";
s.shirt.design = "Orange";
printf("%s
",s.book.title);
printf("%s
",s.shirt.design);
printf("%ld",sizeof(s));
return 0;
}
Char occupies 1 byte right then it's given 8 bytes why so 🙄
it is not a char, it is pointer to char, string literal
Hey snehita are you from bhopal 🧐
wouldn't this mean that when we enter the data for the book structure the shirt structure will also be initialized with the same value. let's say we wanted struct 1 to be about fiction and struct 2 to be about non-fiction. when we initialize the fiction with author and title and price wouldn't the non-fiction become initialized with false data that doesn't belong to it?
E Musk and J. Bezos: Unions? we don't do that here, not accepted in interviews.
I think, this case study is useless. We can create two struct book and shirt . Right?
depois de Paulo Coelho eu parei de prestar atenção kkkkkkkkk
Teach this
to theGoogle Chrome Programmers 😂