What makes them any different from structures?
And how could they possibly be of use?
What makes them any different from structures?
And how could they possibly be of use?
Union are different from structures only in memory layout....
I think code spinet can speak more then thousands of words look below
outputCode:#include <stdio.h> struct structA { int a; int b; int c; }struct_A; union unionA{ int a; int b; int c; }union_A; int main() { struct_A.a = 10; struct_A.b = 20; struct_A.c = 30; printf("struct_A.a == %d\n", struct_A.a); printf("struct_A.b == %d\n", struct_A.b); printf("struct_A.c == %d\n", struct_A.c); union_A.a = 10; printf("union_A.a == %d\n", union_A.a); printf("union_A.b == %d\n", union_A.b); printf("union_A.c == %d\n", union_A.c); union_A.b = 20; printf("union_A.a == %d\n", union_A.a); printf("union_A.b == %d\n", union_A.b); printf("union_A.c == %d\n", union_A.c); union_A.c = 30; printf("union_A.a == %d\n", union_A.a); printf("union_A.b == %d\n", union_A.b); printf("union_A.c == %d\n", union_A.c); return 0; }
struct_A.a == 10
struct_A.b == 20
struct_A.c == 30
union_A.a == 10
union_A.b == 10
union_A.c == 10
union_A.a == 20
union_A.b == 20
union_A.c == 20
union_A.a == 30
union_A.b == 30
union_A.c == 30
i think u will understand
- They can save memory, probably not important to you personally unless you're writing embedded stuff.
- Unions allow you to access the same data in different ways, easier than using bitwise operations and doing it the long way. I'm sure glad Win32's ULARGE_INTEGER is a union, or I'd be hating life right now.
- If used carefully they can confuse the hell out of anyone who wants to modify your code. Job security.
- And speaking of jobs, they make for interesting interview questions. If you're into that sort of thing.
- They're just fun!
I'll paste the union I referred to above just to demonstrate how unions can make things easier:
Code:typedef union _ULARGE_INTEGER { struct { DWORD LowPart; DWORD HighPart; }; DWORDLONG QuadPart; } ULARGE_INTEGER;
See the memory offset in union is same for every one but this is not in the case of structures where. union share the same memory for every element
Thanks, I think with a lil' messing around with it testing concepts I understand my originial questions.
Unions are extremely similar to structures except they allow one set of storage for all members in union
and allow only one member at an given time to be used to store a value
basically it allows for the choice of multiple different types of data, and is much smaller than huge structure
Only one way is valid at any given time
I dislike it when tutorials say this. Yeah I know what they're trying to convey, but it can lead a person to think that they can't set one member and then immediately read a different member of the union or you'll get garbage. This is not the case.
I use unions 100x more frequently for splitting apart packed data then I do for this "one way at a time" stuff.
See hugo2x,
"allow only one member at an given time to be used to store a value
basically it allows for the choice of multiple different types of data, and is much smaller than huge structure"
i m not agree with u with this statement
see union are there for memory distribution among the members
lets say
if u say obj.i = 10;Code:union unionE { char c; int i; }obj;
and u want to fill something in c as well which is relevant it will be filled automatically by the above line of statement
It doesn't matter if you agree with it or not. It's correct. You do not......automatically fill out c as well. You can not store two independant values. You pick one member and use it. If you happen to use another member with the data in it, that's fine. But doing so potentially alters the data in all other members. You cannot store two different values at the same time.
Unions are basically a single memory map, the size of the largest member, and you can access portions of that map by the various handles provided by member names. You cannot store multiple independent values as you would in a structure.
Quzah.
Hope is the first step on the road to disappointment.