/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <stdint.h>
uint8_t test1[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a};
union packet_t {
uint8_t raw[10];
struct {
union {
uint16_t number;
uint8_t number_byte[2];
};
union {
uint32_t size;
uint16_t size_word[2];
uint8_t size_byte[4];
};
uint8_t body[4];
};
} packet;
int main()
{
int i = 0;
for(i = 0; i < sizeof(test1); i++)
{
packet.raw[i] = test1[i];
}
printf("packet.size_word[0]=0x%04x\n", packet.size_word[0]);
printf("packet.size_word[1]=0x%04x\n", packet.size_word[1]);
printf("size of union=%d\n", sizeof(packet));
return 0;
}