# Thread: Unions & Structs... Puzzled.

1. ## Unions & Structs... Puzzled.

Can someone explain maybe with simple examples about how structs and more importantly nesting a union in a struct works?

right now i have something like~

Code:
```typedef struct Operator
{
char symbol;
int operands;
union
{
void *f;
double (*f0)(void);
double (*f1) (double x);
double (*f2) (double x, double y);
}
}Operator;```
the basic premise is it's a "calculator" of sorts... I'm using the struct to define various evaluations methods i have like addition/subtraction etc... The union comes in because the calculator needs to be versatile and should be able to work with any new functions i put in later. Being able to work with zeroary, unary, and binary operators simultaenously without needing to add new code...

I'm confused about the union completely I'm not really sure how it works. Structs I know a little about.

2. A union basically lays all it's members "on top" of each other - so f, f1, f2, and f3 would occupy the same bit of space. For all other intents and purposes, a union works exactly like a struct. [Obviously, as a consequence of the "all members at the same place", if you change one member variant, all others will automatically change too - and it's quite possible, if you for example mix integer and float types, to get values that make no sense when viewed as a different type].

--
Mats

3. Originally Posted by matsp
A union basically lays all it's members "on top" of each other - so f, f1, f2, and f3 would occupy the same bit of space. For all other intents and purposes, a union works exactly like a struct. [Obviously, as a consequence of the "all members at the same place", if you change one member variant, all others will automatically change too - and it's quite possible, if you for example mix integer and float types, to get values that make no sense when viewed as a different type].

--
Mats
I understand that much thank you... I'm more wondering how it works when I declare a struct... How do I work my evaluate functions in there?

I have a function

Code:
```double max1 (double x, double y)
{
return x>y?y:x;
}```
for example that determines the maximum of two numbers and it's a binary operation...

Usually you would put:
Code:
`Operator blah={'M',2,max1};`
M is my symbol.. a char, 2 the # of operators, and max1 the function... Where/how does the union fit in?

4. Your Operator blah looks correctly initialized, and max1 is the value of the union.

--
Mats

5. Originally Posted by matsp
Your Operator blah looks correctly initialized, and max1 is the value of the union.

--
Mats

Hmm I think I see, and since it takes two operators I can call it with *f2 ?

6. Originally Posted by Sparrowhawk
Hmm I think I see, and since it takes two operators I can call it with *f2 ?
Yes.

--
Mats

7. Originally Posted by matsp
Yes.

--
Mats
Hmm, how would this be useful in implementing calculator-like functions?

If I make a bunch of struct definitions, each succeeding one will "overwrite" the union with its own function...

How would it work ?

8. It will actually always use the first member of the union in that case. To access a different member, you would have to use '.' notation.

However in this case since sizeof(void*) and sizeof( double (*) (double ) ) are usually the same, the max passed will be usable with .f2, assuming that your compiler lets you compile that.

9. Originally Posted by Sparrowhawk
Hmm, how would this be useful in implementing calculator-like functions?

If I make a bunch of struct definitions, each succeeding one will "overwrite" the union with its own function...

How would it work ?
What? Each member object gets its own data. So if you had Operator blah, bar, foo, baz; then each one would get its own symbol, its own operands, and its own union.

10. Originally Posted by Sparrowhawk
Hmm, how would this be useful in implementing calculator-like functions?

If I make a bunch of struct definitions, each succeeding one will "overwrite" the union with its own function...

How would it work ?
The code you have will alow you to have a single container, such as an array, of all of your zero-ary, unairy, and binary functions. To actually use the functions you would have to also know the arguements to pass to it. Then you can check if the number of arguments match the Operator, and if they do call the function.

11. Originally Posted by tabstop
What? Each member object gets its own data. So if you had Operator blah, bar, foo, baz; then each one would get its own symbol, its own operands, and its own union.
Oh, of course... Heh heh guess I confused myself there for a second...