On your system, an int requires alignment on a 4-byte boundary. To insure that this requirement is met, the compiler will align the struct on a 4-byte boundary. Since it is possible to create an array of struct and each element of the array must start exactly sizeof(struct) bytes beyond the previous element, the struct must occupy a multiple of 4 bytes.
In your first example:
- varb will be at offset 0 from the start of the struct
- varc will be at offset 4
- vard at offset 8
- vare at offset 9
12 is obviously the smallest multiple of 4 the 10 bytes occupied by the members of lthe struct.
In your second example, varc is at offset 0 and the remaining members are at offsets 4, 5, and 6. 8 is obviously the smallest multiple ....
The most compact way to organize an array is to specify the members in decreasing order of alignment.
No, the compiler cannot reorder the members. The standard requires the members to appear in the struct in the order they are specified.