+ 3
What's the difference between union and struct 🤔
2 odpowiedzi
+ 8
In terms of syntax both are similar....
But main difference is in terms of memory management...
In structure.. All members occupy memory and u can assign value to all or few of them... i.e a structure variable will have all data members.
In union... Memory is allocated for largest member and u can assign or use ONLY ONE of many members...
Say u have members as:
Struct a{
int a;
char b;
double c;
};
And u declare struct a myVar;
Then size is 4+1+8
While for union with similar declaration only max memory ie 8 byte will be allocated..... Cause double occupies max memory out of the three....
And then u can store value for 1 member only... either for int or char or double
+ 4
Although Saurabh B explained it nicely but still if you need more information, just tap the link below
https://www.google.com/amp/s/www.geeksforgeeks.org/difference-structure-union-c/amp/