Posted by Driver on 03:10:00 04-01-2002
Hey guys, was wondering if someone could help me better understand Unions. . . . haven't used them a lot, but I want to try them out. There aren't any tutorials though!
Posted by MoX on 10:38:00 04-01-2002
A Union is a kind of struct, but in memory its variables are not positioned besides each other but they 'share' the same memory. So if you have a undion like
union test_un {
int i;
char teststring[8];
};
you get a 8 byte union. The integer lies at the start of teststring. You can use that for example when you first need an integer and afterwards a string, like a variable which you can assign two different data types to.
I haven't used them yet, so this explanation might be not the best
[addsig]
Posted by Peter on 18:15:00 04-01-2002
thx for that.. can anyone supply some practical uses for it?
Posted by Driver on 22:54:00 04-01-2002
This is the latest structure I have been using. . . it impliments a Union:
struct Node {
char * name;
char nodeType;
struct Node * next;
union {
struct {
long size;
} file;
struct {
struct Node * contents;
struct Node * parent;
} dir;
} nodeData;
};
Then you only have 1 structure, for 2 possible cases, a directory(dir) or a file.
Posted by -KEN- on 22:14:00 04-02-2002
-->-->you get a 8 byte union
since a char by itself is 8 bytes, you'd actually get an 64(8*8) byte union
[addsig]
Posted by MoX on 07:45:00 04-03-2002
-->-->you get a 8 byte union
since a char by itself is 8 bytes, you'd actually get an 64(8*8) byte union
_________________
-Ken
I think I know who's going to be Coder Of the Month this time...
-KEN-, I really have to say you surprise me...Never have I heard greater bullshit. Maybe a char is 8 [b]bit[/] but never ever in life it's 8 byte sized.
Ain't that the proof that windows users loose the sight for the internals?
[addsig]
Posted by Peter on 07:53:00 04-03-2002
That's a funny error, but I would not generalize because of that... If you take the _average_ windows user, perhaps, but this is the ypn..
Posted by MoX on 08:15:00 04-03-2002
I know that Peter, but -KEN- provoked the Linux community so many times that I just wanted to use this opportunity to kick back a little
Don't take it too serious -KEN-!
[addsig]
Posted by -KEN- on 22:19:00 04-03-2002
It was a joke, but ok...
I wanted to find a rolleyes smiley to add to that post, but this board doesn't have a little smiley selection panel, so it was kinda hard to denote the joke...
Oh well...
[addsig]
Posted by MoX on 06:55:00 04-04-2002
-->--> It was a joke, but ok...
No, I don't think so...
-->--> I wanted to find a rolleyes smiley to add -->--> to that post, but this board doesn't have -->--> a little smiley selection panel, so it
-->--> was kinda hard to denote the joke...
How about opening the FAQ in another window and taking the right smiley code frome there???
So, making a dumb mistake is one thing, and it happens to the best, so forget it. But saying it was a joke afterwards...I don't know...
[addsig]
Posted by -KEN- on 23:14:00 04-04-2002
-->-->[some rambling on FAQs]
Considering I barely frequent these boards, ask me if I knew this
And it really was a joke...But then again, like I really care what you think?
[addsig]
Posted by Yjo on 18:23:00 04-06-2002
A union is a user-defined data type that can hold values of different types at different times. It is similar to a structure except that all of its members start at the same location in memory. A union variable can contain only one of its members at a time. The size of the union is at least the size of the largest member.