Say i have the following struct:
typedef struct MyStruct {
unsigned short a; /* 16 bit unsigned integer*/
unsigned short b; /* 16 bit unsigned integer*/
unsigned long c; /* 32 bit unsigned integer*/
}MY_STRUCT;
And some data array (the content only for demonstration):
unsigned short data[] = {0x0011, 0x1100, 0x0001, 0x0FFF };
Then i perform the folliwing:
MY_STRUCT *ms;
ms = (MY_STRUCT *) data;
printf("a is: %X\n",(*ms).a);
printf("b is: %X\n",(*ms).b);
printf("c is: %X\n",(*ms).c);
I would expect the data to be read sequentially into ms, "left to right", in which case the output to be:
a is: 11
b is: 1100
c is: 10FFF
However what actually happens is:
a is: 11
b is: 1100
c is: FFF0001
Why does this happen? What behavior should i expect when casting arrays to structs this way?