struct with variable length member
Consider the code below.
Why is the output wrong when i define a3 of struct AAA as a DWORD, but when it is defined as a WORD, output is ok?
#include <windows.h>
#include <stdio.h>
BYTE buffer[]{0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd};
//#pragma pack(2)
typedef struct {
WORD a1;
WORD a2;
DWORD a3; //<-----this gives wrong output
}AAA;
typedef struct {
WORD b1;
AAA b2[1];
} BBB;
BBB*bbb = (BBB*)buffer;
void main() {
//with a3 as a WORD : 2211 4433 6655
//with a3 as a DWORD: 2211 6655 8877 <----this is wrong, why?
printf("%x %x %x", bbb->b1, bbb->b2[0].a1, bbb->b2[0].a2);
}