structures compatibility

Discussion in 'C Programming' started by =?ISO-8859-1?Q?Sa=EFd?=, Oct 21, 2003.

  1. Hi,

    If I define two structures like this

    struct s1 {

    int a;
    int b;
    }

    struct s2 {
    int a;
    int b;
    int c;
    }

    and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
    structure.

    Can I use ps2 as a pointer on an s1 sturcture and have the a and b
    correct? In ther words, does the order of the different data in a
    structure follow the order in the declaration?

    Thanks.

    --
    Saïd.
    =?ISO-8859-1?Q?Sa=EFd?=, Oct 21, 2003
    #1
    1. Advertising

  2. Saïd <> scribbled the following:
    > Hi,


    > If I define two structures like this


    > struct s1 {


    > int a;
    > int b;
    > }


    > struct s2 {
    > int a;
    > int b;
    > int c;
    > }


    > and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
    > structure.


    > Can I use ps2 as a pointer on an s1 sturcture and have the a and b
    > correct? In ther words, does the order of the different data in a
    > structure follow the order in the declaration?


    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 Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Normal is what everyone else is, and you're not."
    - Dr. Tolian Soran
    Joona I Palaste, Oct 21, 2003
    #2
    1. Advertising

  3. Joona I Palaste wrote:
    > Saïd <> scribbled the following:
    >
    >>Hi,

    >
    >
    >> If I define two structures like this

    >
    >
    >>struct s1 {

    >
    >
    >>int a;
    >>int b;
    >>}

    >
    >
    >>struct s2 {
    >>int a;
    >>int b;
    >>int c;
    >>}

    >
    >
    >>and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
    >>structure.

    >
    >
    >> Can I use ps2 as a pointer on an s1 sturcture and have the a and b
    >>correct? In ther words, does the order of the different data in a
    >>structure follow the order in the declaration?

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


    And what about a specific implementastion as gcc for linux?

    --
    Saïd.
    =?ISO-8859-1?Q?Sa=EFd?=, Oct 21, 2003
    #3
  4. Saïd <> scribbled the following:
    > And what about a specific implementastion as gcc for linux?


    They are off-topic for comp.lang.c. For gcc, there's gnu.gcc.help to ask
    in.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The trouble with the French is they don't have a word for entrepreneur."
    - George Bush
    Joona I Palaste, Oct 21, 2003
    #4
  5. =?ISO-8859-1?Q?Sa=EFd?=

    Eric Sosman Guest

    Joona I Palaste wrote:
    >
    > Saïd <> scribbled the following:
    > > Hi,

    >
    > > If I define two structures like this

    >
    > > struct s1 {

    >
    > > int a;
    > > int b;
    > > }

    >
    > > struct s2 {
    > > int a;
    > > int b;
    > > int c;
    > > }

    >
    > > and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
    > > structure.

    >
    > > Can I use ps2 as a pointer on an s1 sturcture and have the a and b
    > > correct? In ther words, does the order of the different data in a
    > > structure follow the order in the declaration?

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

    --
    Eric Sosman, Oct 21, 2003
    #5
  6. =?ISO-8859-1?Q?Sa=EFd?=

    xarax Guest

    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!


    "Eric Sosman" <> wrote in message
    news:...
    Joona I Palaste wrote:
    >
    > Saïd <> scribbled the following:
    > > Hi,

    >
    > > If I define two structures like this

    >
    > > struct s1 {

    >
    > > int a;
    > > int b;
    > > }

    >
    > > struct s2 {
    > > int a;
    > > int b;
    > > int c;
    > > }

    >
    > > and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
    > > structure.

    >
    > > Can I use ps2 as a pointer on an s1 sturcture and have the a and b
    > > correct? In ther words, does the order of the different data in a
    > > structure follow the order in the declaration?

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

    --
    xarax, Oct 21, 2003
    #6
  7. Okay why does the OP's structs not have a semi-colon after the ending brace
    and the struct posting of xarax does have semi-colons?

    Bill





    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
    Bill Cunningham, Oct 21, 2003
    #7
  8. Bill Cunningham <nospam@net> scribbled the following:
    > Okay why does the OP's structs not have a semi-colon after the ending brace
    > and the struct posting of xarax does have semi-colons?


    They should both have ending semicolons. The OP made a typo.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
    - Guardian in "Jinxter"
    Joona I Palaste, Oct 22, 2003
    #8
  9. Joona I Palaste wrote:
    > Bill Cunningham <nospam@net> scribbled the following:
    >
    >>Okay why does the OP's structs not have a semi-colon after the ending brace
    >>and the struct posting of xarax does have semi-colons?

    >
    >
    > They should both have ending semicolons. The OP made a typo.
    >


    OP means me? Yes I was just presenting the structures, not actually
    coding them. :)

    I'll add a union to make sure the two structures have the same beginig
    even if i don't use this union in my programs.

    Thanks to every body.

    --
    Saïd.
    =?ISO-8859-1?Q?Sa=EFd?=, Oct 22, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ashish
    Replies:
    1
    Views:
    558
    Eric Cadwell
    Nov 6, 2003
  2. Davo
    Replies:
    0
    Views:
    1,930
  3. Zak

    bitstream compatibility

    Zak, Oct 21, 2003, in forum: VHDL
    Replies:
    0
    Views:
    456
  4. tweak
    Replies:
    14
    Views:
    2,777
    Eric Sosman
    Jun 11, 2004
  5. Alfonso Morra
    Replies:
    11
    Views:
    710
    Emmanuel Delahaye
    Sep 24, 2005
Loading...

Share This Page