This is because C and C++ compilers pad structures by adding one or more bytes in between. This is done for efficiency of data access.
For example, in the first case the compiler added two bytes between the two-byte MN
member and the fileSize
, which is an int
, presumably because in your target architecture it is faster to access an int
when its address is divisible by 4.
Note: you can use offsetof(struct BMP_Header, fileSize)
instead of doing the pointer manipulation to compute the offset of the corresponding element of the struct
.