I think a more reliable way to achieve the OP's
intent is to imbed the first struct within the
second struct.
struct s1 {
int a;
int b;
};
struct s2 {
struct s1 s1;
int c;
};
Then passing a s2 pointer as a s1 pointer should
yield the desired behavior.
--
----------------------------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
303-774-9381
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Joona said:
The order does, but the offsets don't have to. IOW, you are guaranteed
that the fields in struct s1 begin in the order a, b and the fields in
struct s2 begin in the order a, b, c. You are furthermore guaranteed
that the field a in struct s1 begins at the same offset as the field a
in struct s2.
You aren't guaranteed anything else. Most importantly, you aren't
guaranteed that the field b in struct s1 begins at the same offset as
the field b in struct s2. There might be a difference of thousands of
bytes, given a particularly nasty implementation.
Therefore the answer to whether you can use pointers to struct s2 as
pointers to struct s1 is "it depends on your implementation".
If struct s1 only consisted of the field a, the answer would be "yes,
knock yourself out". But as it has more fields the answer is not so
simple.
Joona is right (I think), but as a practical matter you'll
find that offsetof(struct s1, b) == offsetof(struct s2, b).
The guarantee of equal offsets *does* hold if there's a
union with the two structs as elements:
union u { struct s1 s1; struct s2 s2; };
.... because there's a special rule in the Standard to cover
this case. Now, the practical difficulty: How can a compiler
determine that no such union exists? Even if no such union
exists in the current translation unit, there might be such
a union declared in another translation unit somewhere else,
perhaps in source code that hasn't even been written yet but
will be written and compiled tomorrow and then linked with
this one. So a compiler would need to be almost supernaturally
clever to determine whetherr it was safe to let the offsets
disagree -- and the people who write compilers have more
important things to spend their time on than deviousness and
perversity (it doesn't always seem that way, but ...)
Summary: Technically and legalistically, you have no
guarantee about the offsets. In practice, though, you do.