Hello,
I have a question about the layout of the fields in a structure. I have
read
in a paper that if two structures contain an initial sequence of
fields,
all of which have compatible types, then the offsets of the
corresponding
fields in initial sequence are guaranteed to be the same. For example,
with
typedef struct {
int a;
int b;
char c;
} s1;
typedef struct {
int a;
int b;
int d[2];
} s2;
s1 t1;
s2 t2;
then the fields t1.b and t2.b have the same offset. I know that,
according
to the ISO specification (ISO/IEC 9899:1999, §6.5.2.3), this happens
when
we consider two elements of type s1 and s2 in a union, for example :
union {
s1 t1;
s2 t2
} a;
Is it true when t1 and t2 are not fields of a union ?
Thanks !
Regards.
Xavier
I have a question about the layout of the fields in a structure. I have
read
in a paper that if two structures contain an initial sequence of
fields,
all of which have compatible types, then the offsets of the
corresponding
fields in initial sequence are guaranteed to be the same. For example,
with
typedef struct {
int a;
int b;
char c;
} s1;
typedef struct {
int a;
int b;
int d[2];
} s2;
s1 t1;
s2 t2;
then the fields t1.b and t2.b have the same offset. I know that,
according
to the ISO specification (ISO/IEC 9899:1999, §6.5.2.3), this happens
when
we consider two elements of type s1 and s2 in a union, for example :
union {
s1 t1;
s2 t2
} a;
Is it true when t1 and t2 are not fields of a union ?
Thanks !
Regards.
Xavier
Comment