"United We Stand"

There is a relative of the C structure called the union . We won't be needing unions this semester, but we describe them here for completeness.

If we declare something much as we do a structure but with the keyword union instead of struct , then we have a union. For example, if we have


union thing {
   int i;
   float f;
   char c;
};

and
struct thing x;

we find that x.i is an integer, x.f is a floating point number and x.c is a character just as with a structure. The difference is that we can't store them all at once. We can have the integer, the floating point number or the character but only one of them at a time.

As you might guess, the union has a somewhat more specialized use as opposed to the structure. For example, we might use it if we had an array of structures that were all the same, but one part of them sometimes had an integer and sometimes had a float. Then we'd declare a union to be a member of the structure we created an array of. You can probably see why we're not going to be using unions in this class.


Click here to continue to the next part.