Unions
A union is a type whose members share one region of storage. The size is the size of the largest member, plus tail padding. Only one member holds a value at a time.
Detailed Explanation
union Word { uint32_t u; uint8_t b[4]; }; occupies four bytes, not eight. Writing w.u = 0x01020304; and then reading w.b[0] inspects those bytes. Which byte is the least significant depends on endianness. That overlay is why unions show up in protocol code, in device registers, and in any place you have one storage slot that must hold several shapes.
The standard rule is: the member you last wrote is the one you may read. Reading a different member is unspecified in older C and, for a few special cases, defined in C99 as a way to reinterpret the bytes. In practice compilers treat union type-punning as supported, and it is one of the two blessed ways to re-read an object's representation. The other is memcpy into an object of the other type. A pointer cast between incompatible types is not blessed; that is a strict-aliasing violation.
A union does not remember which member is live. You do. The usual wrapper is a struct with a tag enum and a union:
struct Value {
enum { VAL_INT, VAL_PTR } kind;
union {
int i;
void *p;
} u;
};
Every write updates kind. Every read switches on kind. Forget the tag and you have a quieter version of a use-after-free: the bits are still there, they just do not mean what you think.
Anonymous unions, allowed as a common extension and standardized later, let you write v.i instead of v.u.i when the union is an unnamed member of a struct. They do not change the layout rule.
Initialization applies to the first named member unless you use a designated initializer: union Word w = { .b = { 1, 2, 3, 4 } };. A zero initializer {0} clears the first member and, in practice, the whole object.
Code Examples
#include <stdio.h>
#include <stdint.h>
union Word {
uint32_t u;
uint8_t b[4];
};
int main(void)
{
union Word w;
w.u = 0x01020304u;
printf("%02x %02x %02x %02x\n", w.b[0], w.b[1], w.b[2], w.b[3]);
/* little-endian host prints: 04 03 02 01
big-endian host prints: 01 02 03 04 */
return 0;
}#include <stdio.h>
#include <stddef.h>
enum Kind { VAL_INT, VAL_DOUBLE };
struct Value {
enum Kind kind;
union {
int i;
double d;
} u;
};
static void print_value(struct Value v)
{
switch (v.kind) {
case VAL_INT: printf("%d\n", v.u.i); break;
case VAL_DOUBLE: printf("%.1f\n", v.u.d); break;
}
}
int main(void)
{
struct Value v = { .kind = VAL_DOUBLE, .u.d = 2.5 };
print_value(v); /* prints: 2.5 */
printf("sizeof union part=%zu\n", sizeof v.u);
return 0;
}Common Gotchas
Reading a member you did not write, without intending type-punning, is a logic bug
the bits are the previous member's representation. Keep a tag.
Endianness decides what overlaying an integer on a byte array means
test on both, or write bytes in a defined order with shifts.
A union is not a safe variant type by itself
it will not stop you from storing an `int` and reading a pointer. The struct-plus-enum wrapper is the actual type.
`memcpy` is the strictly portable reinterpret
use it when you must also satisfy compilers that are strict about union punning, or when the destination is not a union member.