What is union (C union type)?

A C type whose members all start at the same address and overlap. It is declared as in union { uint32_t w; uint8_t b[4]; } u; and its size is that of the largest member (rounded up to a multiple of the alignment). Only one member holds a meaningful value at a time; unions are used to view the same memory as different types, and to save memory when data takes one of several forms. Writing u.w and reading u.b[0] gives a result that depends on endianness.

Japanese name: 共用体

union (C union type) appears in 1 practice question across 1 topic, including C Programming Basics.

Field: C Programming Basics

union (C union type) in the explanations

Questions that mention union (C union type) (1)

  1. uint32_t and uint8_t are 32-bit and 8-bit unsigned integer types, and the environment is little-endian. Which of the following is the output when the code above is executed?

Books on C Programming Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Alignment and padding Bit-field Buffer overflow Dangling pointer Dynamic memory allocation Function pointer Integer promotion Macro (#define) Memory leak Pointer

Back to the glossary