W
Walter Roberson
typedef struct USB_iobuf_common_header_s {
unsigned char function;
unsigned char size_lo;
unsigned char size_hi;
} USB_iobuf_common_header;
typedef struct USB_iobuf_i2c_header_s {
unsigned char addr;
unsigned char subaddr_lo;
unsigned char subaddr_hi;
unsigned char datasize_lo;
unsigned char datasize_hi;
} USB_iobuf_i2c_header;
reg = data[sizeof(USB_iobuf_common_header) +
sizeof(USB_iobuf_i2c_header)];
At this point I expect to receive data in 'reg', but it's a junk I'm
getting. What's the problem with that?
C compilers are permitted to (and in practice many do) put trailing
padding in structures so that arrays of the structures would have
the elements align properly. The sizes you would naively expect
from the above structures is 3 characters and 5 characters respectively,
but it would not be at all unusual for padding to be tossed in causing
the first structure to be 4 characters and the second to be 8 characters.
You probably intend that 'addr' follows immediately after 'size_hi'
in the overall message array, but an indeterminate amount of padding
could occur between them. sizeof() a structure is the size including
any extra padding, rather than the sums of the sizes of the visible
elements.
Probably what you will have to end up doing is using a union
after size_hi .