Quantcast
Viewing all articles
Browse latest Browse all 4

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

#include<stdio.h>

#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.


Viewing all articles
Browse latest Browse all 4

Trending Articles