On Fri, 2012-07-27,
wrote:
> Hello Experts,
> suppose i have a packet of 9 bytes having fields: length(4bytes),
> type(1 byte), version(4bytes). Now i would write the packet structure
> as
> struct mypack {
> unsigned int;
> unsigned char;
> unsigned int;
> };
My standard answer: don't use structs for I/O. Instead define the I/O
data format in terms of octets (e.g. if this is something UDP-based)
and write functions for filling in/reading from an uint8_t buffer
according to that format.
You can still have a struct, but it won't neccessarily match,
bit-by-bit, the data in your I/O.
Doing the struct-mapping thing IME leads to all kinds of problems:
compiler dependencies, endianness bugs, buffer overflows, weakly
defined data formats, lots of casting, valgrind warnings ...
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .