Structures lay out their members sequentially in memory, and it's meant to be meaningful to access any and all of their members. If you had :
struct Data_u
{
long x;
char ch[20];
};
Then you would expect to be able to access x
and ch
, independently and correctly. In general, the size of a structure is the size of the sum of its fields (plus padding), which looks like what you tried to calculate.
However, this is a union, not a structure. Unions are meant to represent just one of their fields, and you somehow decide which one it makes sense to use. The other members should be considered invalid. Because only one member is meant to be valid at a time, it would be wasteful to have them laid out sequentially in memory, so in most cases every member of a union is aligned at the start of the union in memory. This means that in general, the size of a union is the size of its largest member (plus padding).
In your case, the largest member is the char array, independent of bitness. In a 32-bit program, the size would most likely be 20 bytes because that's how big the array is. In a 64-bit program, because of padding, that could be bumped up to 24 bytes.