0

size of int in my system is 4 bytes and char is 1 byte ,so I thought size of this struct will be 10(4+6) bytes.

struct Riddle {
    int n;
    char s[6];
};

but it is 12 bytes(checked with printf("%d",sizeof(Riddle)).

this might be a stupid question ,but it really confused me.

can you explain to me how is it possible?

thanks for help.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
hanie
  • 1,863
  • 3
  • 9
  • 19
  • 2
    struct size is platform dependent. Consult the reference manual for your particular compiler to find out if and how your platform pads bytes in struct layouts. – Patrick Roberts Mar 17 '20 at 14:10
  • The size of the struct is how big it needs to be for an array of same. That is affected by the alignment requirement of the components of the struct. In this case I guess the `int` is 4 byte aligned, so the size needs to be a multiple of 4. – Chris Hall Mar 17 '20 at 14:19

0 Answers0