SunStudio C/C++ compiler can not initialize the pointer member within a packed structure


#pragma pack(1)

struct foo{
        char a;
        char *str;

int main()
        struct  foo bar={'a', "test"};
        printf ("%p, %p\n", &(bar.a), &(bar.str));

$ cc test.c
"test.c", line 12: cannot use an address to initialize a field of a packed struct (#pragma pack)

While, gcc (even on sparc) could compile this test program, and from the output we could see that the "str" member just follows the char member -- 'a', and does not align with the 4/8 byte boundary. So it's not limitation of architecture, but a constraint of compiler.

Leave a Reply

Your email address will not be published. Required fields are marked *

To submit your comment, click the image below where it asks you to...
Clickcha - The One-Click Captcha