C++
-
A union is essentially a structure in which all of the fields overlay each other; you can only use one field at a time. But that's not the case with structures. In structures fields don't overlay each other and any field can be used any time. The size of a union is the maximum of the sizes of its individual members, while the size of a structure is the sum of the sizes of its members.
Nibu thomas Software Developer
-
Each field of a union starts at the same location in memory, whereas each field in a structure gets separate memory locations.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Just to explain so that you can get a clear view of what the difference is: suppose we have a structure struct abc { int a; char b; }a1; When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes and a char value of 1 byte . The total size of the structure would thus be 3 On the other hand if you use unions and declare it as union def{ int a; char ch; }u1; Then the size of operator will show 2. As the memory of variables in a union is shared and the size is of the highest occupant in the union. Vision is Always important and so is your ATTITUDE. Wishes. Anshuman Dandekar
-
Just to explain so that you can get a clear view of what the difference is: suppose we have a structure struct abc { int a; char b; }a1; When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes and a char value of 1 byte . The total size of the structure would thus be 3 On the other hand if you use unions and declare it as union def{ int a; char ch; }u1; Then the size of operator will show 2. As the memory of variables in a union is shared and the size is of the highest occupant in the union. Vision is Always important and so is your ATTITUDE. Wishes. Anshuman Dandekar
AnShUmAn_VCPP wrote:
When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes...
I think you mean 4 bytes.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
AnShUmAn_VCPP wrote:
When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes...
I think you mean 4 bytes.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
yes . The value that will be shown is compiler dependent. If you work on turbo c you will get 2 if you work on visula studio you will get the size of an int in this case Vision is Always important and so is your ATTITUDE. Wishes. Anshuman Dandekar
-
yes . The value that will be shown is compiler dependent. If you work on turbo c you will get 2 if you work on visula studio you will get the size of an int in this case Vision is Always important and so is your ATTITUDE. Wishes. Anshuman Dandekar
AnShUmAn_VCPP wrote:
If you work on turbo c you will get 2
But this is a Visual C++ forum...
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb