0
`typedef struct emp
{
    char name[2];
    int age;
    double sal;
} e1;

int main(){ e1 employee1; }`

I am expecting the sizeof(employee1) is 14 Bytes. but it is giving me 16 Bytes. I want to know why is ity 16 Bytes.

mohrafik
  • 26
  • 2
  • 1
    It's because an 'int' has to be aligned on an address that is a multiple of the size of an 'int'. In this case it seems that is 4 bytes. So 2 bytes of padding are inserted after 'name' to align 'age' to a 4-byte boundary. You could investigate how to _pack_ your structure if this really matters to you, but you then run the risk of getting bus faults for unaligned accesses (depending on your architecture). – pmacfarlane Dec 21 '22 at 12:32

0 Answers0