Mail Archives: djgpp/1996/04/16/00:41:35
Reply to message 2225083 from ELIZ AT IS DOT ELTA. on 04/15/96 2:46AM
>You should declare ALL the fields with the attribute, like this:
>
> struct foo
> {
> char a __attribute__((packed));
> int x[2] __attribute__((packed));
> };
There's an easier way to do that:
struct foo
{
char a;
int x[2];
} __attribute__((packed));
Just put the __attribute__ at the end of the struct definition, and all
its fields are packed automatically.
I had to do something like this for a program that would be
compiled on both DJGPP and Turbo C. I made all my ints short,
and closed the struct definition with:
#if defined(GNUC)
} __attribute__((packed));
#else
};
#endif
Thus, the resulting structures could be stored by fread() in a format
compatible between both 32-bit and 16-bit compilers, and my data
files wouldn't have to be rewritten every time I switched.
John
- Raw text -