On Fri, Jan 25, 2002 at 07:12:47PM -0800, Phong Nguyen wrote:
> Thanks for the answers so far but if there is no
> solution for this issue, it would really be a SERIOUS
> problem for me and also for everyone out there when
> using the built-in function sizeof(..) !!!!!!! What
> can I do if I want to define several structure types
> defined a structure of a message and try to decode it
> using the structure pointers as follows:
> 
> typedef struct
> {
>     char preamble[2];
>     char command;
> }
> HeaderType;
> 
> typedef struct
> {
>     char checksum[2];
>     char terminator;
> }
> TrailerType;
> 
> typedef struct
> {
>     HeaderType  header;
>     char        data[10];
>     TrailerType trailer;
> }
> MessageType;

sizeof() is not a function, but a C operator.
What you are trying to do, if I understand correctly, is to
use C structures to represent binary messages.
This is definitely not portable and not safe (endianness
and alignment restrictions vary between architectures, and
structure field alignment could vary also between different
compilers for the *same* architecture).
You should really encode/decode your messages by manipulating
sequences of bytes / words / dwords as needed.
For example, reading from a file or the network, you should
do something similar to:

   MessageType msg;

   msg.header.preample[0] = read_byte( fd );
   msg.header.preample[1] = read_byte( fd );
   msg.header.command     = read_byte( fd );
   for (i = 0; i < 10; i++)
       msg.data[i] = read_byte( fd );
   msg.trailer.checksum[0] = read_byte( fd );
   msg.trailer.checksum[1] = read_byte( fd );
   msg.trailer.terminator  = read_byte( fd );

(Ok, the code is somewhat Unix-ish, but you get the point).
And writing is simmetrical.
Obviously you can't rely on sizeof() to obtain the message
size...

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/

Reply via email to