An interesting structure!

Jason Holt jason at lunkwill.org
Mon Nov 28 00:39:07 MST 2005


gcc4 seems to do what you want.


[jason at erg] ~$ gcc --version
gcc (GCC) 4.0.2 (Debian 4.0.2-2)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


[jason at erg] ~$ more foo.c
#include <stdio.h>

typedef char bool;

struct
{
         unsigned short A;
         bool B;
         unsigned long C;
} STR;

main() {
         int i;
         STR.B = 1;
         STR.A = 0x2345;
         STR.C = 0x6789ABCD;
         for(i=0; i<sizeof(STR); i++) {
                 printf("%x ", (unsigned char) (((char *)&STR)[i]));
         }
         printf("\n");
}

[jason at erg] ~$ ./foo
45 23 1 0 cd ab 89 67


[jason at erg] ~$ more bar.c
#include <stdio.h>

typedef char bool;

struct __attribute ((__packed__))
{
         unsigned short A;
         bool B;
         unsigned long C;
} STR;

main() {
         int i;
         STR.B = 1;
         STR.A = 0x2345;
         STR.C = 0x6789ABCD;
         for(i=0; i<sizeof(STR); i++) {
                 printf("%x ", (unsigned char) (((char *)&STR)[i]));
         }
         printf("\n");
}

[jason at erg] ~$ ./bar
45 23 1 cd ab 89 67




More information about the PLUG mailing list