You can use _Packed to qualify a union. However, the memory layout of the union members is not affected. Each member starts at offset zero. The _Packed qualifier does affect the total alignment restriction of the whole union.
C++ does not support the _Packed qualifier. To change the alignment of unions, use the #pragma pack directive or the
compiler option. Both of these methods are also supported by C.
In the following example, each of the elements in the
nonpacked n_array is of type union uu.
union uu { short a; struct { char x; char y; char z; } b; }; union uu n_array[2]; _Packed union uu p_array[2];
Because it is not packed, each element in the array has an alignment restriction of 2 bytes (the largest alignment requirement among the union members is that of short a), and there is 1 byte of padding at the end of each element to enforce this requirement.
Now consider the packed array p_array. Because each of its elements is of type _Packed union uu, the alignment restriction of every element is the byte boundary. Therefore, each element has a length of only 3 bytes, instead of the 4 bytes in the previous example.