Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Struct vs Union (C)

Featured Replies

Posted

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

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;
        struct
        {
            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.

  • Author

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!

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.