X
Xavier Noria
Given two structures of the same size but different type, does C99
guarantee that pointers to them can be casted one to each other, and
that the order of the elements will be kind of respected?
This code illustrates what I mean:
int main(void) {
struct foo { char c[2]; };
struct bar { char c0; char c1; };
struct foo f;
struct bar b, *bp;
f.c[0] = 'a';
bp = (struct bar*) &f; // guaranteed to be valid?
bp->c0 == 'a'; // guaranteed to hold?
}
Do you just access the address and the field offset is always computed
from left to right so that works even with mixed field types?
-- fxn
guarantee that pointers to them can be casted one to each other, and
that the order of the elements will be kind of respected?
This code illustrates what I mean:
int main(void) {
struct foo { char c[2]; };
struct bar { char c0; char c1; };
struct foo f;
struct bar b, *bp;
f.c[0] = 'a';
bp = (struct bar*) &f; // guaranteed to be valid?
bp->c0 == 'a'; // guaranteed to hold?
}
Do you just access the address and the field offset is always computed
from left to right so that works even with mixed field types?
-- fxn