Mail Archives: djgpp/1996/08/18/21:17:19
Thinking furiously, Andreas Vernersson wrote:
>
> Could anyone explain this?
Sure.
> typedef struct a
> { unsigned int q2 __attribute__((packed));
> unsigned char qq __attribute__((packed));
> } b;
> typedef struct a2
> {
> unsigned int q2;
> unsigned char qq;
> } b2;
> void main()
> {
> printf("size: %d\n",(int)sizeof(b)); // 5 ???
> printf("size: %d\n",(int)sizeof(b2)); // 8 ???
> }
> I only wants the datastructure to be 2+1=3 bytes...
An int *is* four bytes. 4 + 1 = 5.
Try doing:
printf ("%d\n", sizeof(int));
and see what it prints.
If you're expecting it to be 3 merely because you believe an int ought
to be (or is) 2 bytes, then you're going to be disappointed.
OTOH, if you REQUIRE that it be 3 bytes (for example if it has to
match a register that's that size on a piece of hardware), then you
need to use a 16-bit intergral type, such as short or unsigned short.
Fred
--
-------------------------------------------------------------------------------
.---- Fred Smith / Office: fred AT computrition DOT com
( /__ ,__. __ __ / __ : / 508-663-2524
/ / / /__) / / /__) .+' Home: fredex AT fcshome DOT stoneham DOT ma DOT us
/ / (__ (___ (__(_ (___ / :__ 617-438-5471
-------------------------------- Jude 1:24,25 ---------------------------------
- Raw text -