I wrote following code
typedef union { struct { int8_t a; int16_t b; uint32_t c; }; int8_t my_arr[7]; }x; uint8_t arr[7]; int main( void ) { uint32_t i; x *y; for( i = 0 ; i < 7 ; i++ ) { arr[i] = i + 2 * 5; } y = ( x* )arr; i = y->a; i = y->b; i = y->c; }
End result in Y is: a = 0x0a; b = 0x0d0c c = 0x00100F0E my_arr[0-7] = 0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10;
& array conatisn arr[0-7] = 0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10;
My question is when a= 0x0a , then should be b = 0x0c0b & c = 0x100f0e0d but this is not the case
I am using keil 4.70.0.0, mdk arm
Try PACK Compiler Directive
Or better still: don't. Numerous people keep suggesting packed data structures as the solution to this "problem", and the suggestion usually comes so quickly and with so little backing argument that it's clear it's really a reflex rather than a considered strategy. Not surprisingly, given this lack of reasoning, the suggested solution is usually wrong.
TOTALLY agree.
I have stated to many "pack should ONLY be used when importing data files where the data is "packed" and using the pack directive makes the data 'slip' into a structure.
Or better still: don't.
I would concur. It's nice to see that everyone, once in a while, can write something sensible.