Message-ID: <32F088E7.34CA@eik.bme.hu> Date: Thu, 30 Jan 1997 12:41:27 +0100 From: "DR. Andras Solyom" Reply-To: solyom AT eik DOT bme DOT hu Organization: Technical University of Budapest MIME-Version: 1.0 To: djgpp AT delorie DOT com Subject: Re: Help: Packing structures References: <01bc0e37$b83989e0$1293d9c2 AT victoria DOT browns DOT co DOT uk> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Victoria Bradburne wrote: > > Please could someone help my in packing a data structure. I am using DJGPP > as a cross compiler to M68000 and am having trouble packing some data > structures: > > struct Packet { > Byte ID; > Word Length; > Byte Data[DATALEN]; > }; > What I always define is: #ifndef PACKED #ifdef __GNUC__ #define PACKED __attribute__ ((packed)) #else #define PACKED #endif #endif and for your case I woud do struct Packet { Byte ID PACKED; Word Length PACKED; Byte Data[DATALEN] PACKED; }; Please take care! If you want to pack any structure you MUST: put this PACKED attribute after EVERY member in your struct and MOST NOT put two variables with one type definition and only one PACKED attribute e.g. : struct any { long l1,l2 PACKED; byte b: PACKED; int i: PACKED; }; is WRONG. The correct way to do it is: struct any { long l1 PACKED; long l2 PACKED; byte b: PACKED; int i: PACKED; }; Note that although longs should always be packed in principle, if you do not put the packed attribute after them then your whole structure will be packed. Eli, is this covered in the FAQ? Andras