Q:

C struct v union in embedded programming - why?

Is there anything that a union can do that a struct cannot?
I know that one might use a union to save memory space, but these days it hardly matters.

A:

The use of a UNION permits formatting a particular storage allocation to different variable types or layouts. This can be used to advantage in a number of ways. Here are a couple of examples.

You have a UNION that defines the various record layouts for packets of data that arrive from a communication channel.
You can use the UNION to efficiently unpack data from one variable type to another such as overlaying a "uint_32" with a "uint_8[4]" to unpack bytes from a dword.

A:

The difference between a Union and a struct already has been explained.
If you know there is only one value (which can have different types) than use a union. optionally you need another variable that keeps track what type the union is. Like:
union 
{
   uint8_t value_8;
   uint16_t value_16;
   uint32_t value_32;
};

enum EUnionType { VALUE_8, VALUE_16, VALUE_36 } _unionType;

The enum _unionType can be set to VALUE_8/16/32 to denote what type the union has.
About memory: Memory IS important, especially for some microcontrollers which have less memory (e.g. Arduino Uno with 2 KB). And if you have 400 of these unions (in an array) it is a difference:

Struct: 4 + 2 + 1 bytes = 7 bytes, per element. Times 400 = 2,800 bytes -> Does not fit in memory.
Union : 4 bytes * 400 = 1,600 bytes -> Fits in memory.

A:

Within a union, every element is stored at the same memory location, e.g.
union{
  uint32_t a;
  uint8_t b[4];
}

If you modify b[], you also modify a, because they're using the same physical adress. It's convenient, if you have use data as different types.
Within structures, each element is places one after the other with different adresses.

