Jump to content
Tuts 4 You

Struct vs Union (C)


Recommended Posts

Good morning.

What is the difference in practical level of Union compared to Struct? In what cases (examples) Union is/should be, used?

-Thank you

Link to comment

Structures are used to hold a 'record' of data. You have multiple different types in a row that can be mapped to a block of memory. The size of the structure is equal to total size of all the element types inside of the structure. (+/- any compiler based padding/alignment settings and enforcements.)

Unions are a type that is designed to allow multiple types to be applied/used with a single piece of memory. The compiler will use the size of the largest type in the union to determine the total size used.

For example:

struct test_t
    uint8_t Element1;  // 1 byte
    uint8_t Element2;  // 1 byte
    uint16_t Element3; // 2 bytes
    uint32_t Element4; // 4 bytes

This is a structure that has 4 elements, with a total size of 8 bytes.

And for a union:

union test_t
    uint8_t ElementByte;
    uint16_t ElementShort;
    uint32_t ElementInt;

This is a union that has the ability to be a ubyte, ushort, or uint but since the uint32_t is the largest type in the union, its total size is 4 bytes.

As for when is it useful to use a union, that will generally depend on your implementation. They can be useful to cut down on the need to write multiple struct definitions for something that can be defined as a single union. It's also useful for usage of a scalar type that may represent all available base types and also hold a 'type' variable telling the user which type is being used/held.

In most cases though, anytime you would write a union, it could just be written as multiple structs to define the same things. It's just helping cut down the need to write 2 or more structures to define something that can be done in a single union.

They are also useful when defining a single type as a bit array but wish to still have an 'overall' value accessor. For example:

    union unityinfo_t
        uint32_t Raw;
            uint32_t Faction : 4;
            uint32_t Unknown : 6;
            uint32_t Points : 22;
        } Bits;

As for other examples, think of something like defining a RECT to tell the area of an object. You can make use of a union to quickly define a RECT type that can be either int32_t, uint32_t, or floats to hold the data allowing you to quickly change between the types or use the same definition for multiple calls expecting different versions.

Rather than write 3 different structures to define each type of RECT, you can define it as a single union type with the inner types.

  • Like 2
  • Thanks 1
Link to comment

Thank you @atom0sfor the very detailed answer! The book i have, it explains only in half page the "unions".

So i see that using a "union" is somehow (little) sophisticated and not so frequent technique for representing user-define data type. Or to say it better is for very specific/special cases, compared to structs. It depends on what you (the programmer) want to do.

Plus one more difference: it stores only one value at the time for all members, and the rest are overwritten - if i understand it correctly.

I think i've got this. I think is good time now (tomorrow i mean), to go deeper in more practical examples :) Thanks again!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...