typedef struct __attribute__((__packed__)) _bitmap
This is the way to define a struct definition without zero paddings in GCC.To prevent zero padding in MVSC, use #pragma pack
:
#pragma pack(push,1)typedef struct _bitmap{ //Error is here on _bitmap uint8_t magicNumber[2]; uint32_t size; uint8_t reserved[4]; uint32_t startOffset; uint32_t headerSize; uint32_t width; uint32_t height; uint16_t planes; uint16_t depth; uint32_t compression; uint32_t imageSize; uint32_t xPPM; uint32_t yPPM; uint32_t nUsedColors; uint32_t nImportantColors;} _bitmap;#pragma pack(pop)
As you can see here https://godbolt.org/z/Gr-Rsw it compiles just fine.
Note that disabling padding is important to get the exact structure you want (first 2 bytes which are "magic number" then 4 bytes of size, etc). In such case you can read first sizeof(_bitmap)
bytes from the file and expect the layout be exactly as defined in the struct.
Without the #pragma pack
, the size of such struct may very based on different architectures. For example here the size is 56 byte, that's because the compiler adds padding after magicNumber[2]
to align it to 4 bytes while with the pack
attribute it is 54 bytes (example).