On Fri, Jan 25, 2002 at 07:59:18PM -0800, Phong Nguyen wrote: > What I want to do is: > > char buffer[100]; > char* message = buffer; > int index = 0; > buildHeader (&message[index]); > index += sizeof(HeaderType); > assembleData (&message[index]); > index += 10; > buildTrailer (&message[index]); > index += sizeof(TrailerType); > > and return index as the size of the built message. As > you can see if sizeof (..) does not return the correct > value, it would mess up the content of the message and > also the return size. Of course, I can use 3 instead > of sizeof(HeaderType) or so but this is not really a > good programming practice since I may want to change > the structure of HeaderType and TrailerType later. On > the other hand I might also want to port existing code > built in other platforms to Palm OS and so if > sizeof(..) does not return the correct value ....my > feeling for CodeWarrior would be "!%$##@!#$%!@#%^$&*" > .
sizeof() here returns the correct value and please note that this is *not* a CodeWarrior issue. Almost every compiler that I know of does alignment on fields within structures, and this is the proper and expected behavior. As I said, you are trying to specify a binary format with the declaration of a structure. This is an error. Structures are not meant for specifying memory layouts. Regarding the programming practice, you should have your binary format documented before writing the code. Then you can write macros to specify things like field length: #define MSG_HEADER_PREAMBLE_SZ 2 #define MSG_HEADER_SZ 3 ... and use these. And you *can't* read whole blocks into structures! You have to read separately individual fields (or parse from memory as in your case). Otherwise, as suggested by others, you can resort to #pragma to specify packed structures, but this is not portable and I'd tend to consider this one a bad programming practice. Marco -- ======================================================================== Marco Pantaleoni [EMAIL PROTECTED] Padova, Italy [EMAIL PROTECTED] elastiC language developer http://www.elasticworld.org -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
