Re: Mutually referential types

Discussion in 'C Programming' started by Eric, Aug 15, 2003.

  1. Eric

    Eric Guest

    Colin JN Breame <> wrote:

    > typedef struct {
    > B *pB;
    > } A;
    >
    > typedef struct {
    > A *pA;
    > } B;


    should be:


    typedef struct A
    {
    struct B *pB;
    } A;

    typedef struct B
    {
    struct A *pA;
    } B;


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
    Eric, Aug 15, 2003
    #1
    1. Advertising

  2. Eric

    signuts Guest

    > I've been using constructs like this, but I still wonder why it
    > works - IOW: how is the compiler able to accept 'struct B *pB'
    > without already having a declaration of 'struct B' at hand????


    Someone please correct me if i'm wrong but I believe it's because
    you're creating a pointer, of type "struct B." There is no need to
    know all the members of the structure in order to do this.

    A pointer is always the same size.. on my system it's 4

    Hope this helps.

    --
    Sig
    Fri Aug 15 14:56:25 EDT 2003
    signuts, Aug 15, 2003
    #2
    1. Advertising

  3. On Fri, 15 Aug 2003, signuts wrote:
    > >
    > > I've been using constructs like this, but I still wonder why it
    > > works - IOW: how is the compiler able to accept 'struct B *pB'
    > > without already having a declaration of 'struct B' at hand????

    >
    > Someone please correct me if i'm wrong but I believe it's because
    > you're creating a pointer, of type "struct B."


    You're creating a pointer of type "pointer to struct B."
    ^^^^^^^^^^

    > There is no need to
    > know all the members of the structure in order to do this.


    Right. In fact, your compiler *will* complain if you try
    to access the members of an object of type 'struct B' before
    'struct B' is defined, for precisely this reason. But since
    you don't need to know the layout of 'struct B' in order to
    point to it, everything's fine.

    > A pointer is always the same size.. on my system it's 4


    Not necessarily. The C standard doesn't even mandate that
    all pointers *to structures* be the same size - maybe
    sizeof(struct foo*)==4 and sizeof(struct bar*)==8. But
    in practice, most compilers will make all struct pointers
    the same size because it makes their job easier.

    -Arthur
    Arthur J. O'Dwyer, Aug 15, 2003
    #3
  4. Eric

    Eric Sosman Guest

    "Arthur J. O'Dwyer" wrote:
    >
    > On Fri, 15 Aug 2003, signuts wrote:
    > > >
    > > > I've been using constructs like this, but I still wonder why it
    > > > works - IOW: how is the compiler able to accept 'struct B *pB'
    > > > without already having a declaration of 'struct B' at hand????

    > >
    > > Someone please correct me if i'm wrong but I believe it's because
    > > you're creating a pointer, of type "struct B."

    >
    > You're creating a pointer of type "pointer to struct B."
    > ^^^^^^^^^^
    >
    > > There is no need to
    > > know all the members of the structure in order to do this.

    >
    > Right. In fact, your compiler *will* complain if you try
    > to access the members of an object of type 'struct B' before
    > 'struct B' is defined, for precisely this reason. But since
    > you don't need to know the layout of 'struct B' in order to
    > point to it, everything's fine.
    >
    > > A pointer is always the same size.. on my system it's 4

    >
    > Not necessarily. The C standard doesn't even mandate that
    > all pointers *to structures* be the same size - maybe
    > sizeof(struct foo*)==4 and sizeof(struct bar*)==8. But
    > in practice, most compilers will make all struct pointers
    > the same size because it makes their job easier.


    6.2.5 Types
    /26/ [...] All pointers to structure types shall have
    the same representation and alignment requirements as
    each other. [...]

    .... so "most compilers" is a fairly inclusive set.

    --
    Eric Sosman, Aug 15, 2003
    #4
  5. Eric

    Mark Haigh Guest

    signuts wrote:
    >>I've been using constructs like this, but I still wonder why it
    >>works - IOW: how is the compiler able to accept 'struct B *pB'
    >>without already having a declaration of 'struct B' at hand????

    >
    >
    > Someone please correct me if i'm wrong but I believe it's because
    > you're creating a pointer, of type "struct B." There is no need to
    > know all the members of the structure in order to do this.
    >
    > A pointer is always the same size.. on my system it's 4


    This is misleading; all pointers may not, in fact, be the same size.
    However, pointers to incomplete types must be of the same size as
    pointer to void (which is simply an incomplete type that cannot be
    completed).

    Otherwise you would have chaos as different incomplete types in
    different translation units could choose representations of differing size.



    Mark F. Haigh
    Mark Haigh, Aug 17, 2003
    #5
  6. Eric

    Mark Haigh Guest

    > Otherwise you would have chaos as different incomplete types in
    > different translation units could choose representations of differing size.


    Oops, should be:

    Otherwise you would have chaos as pointers to different incomplete types
    in different translation units could have representations of differing size.

    Time for another beer :)

    Mark F. Haigh
    Mark Haigh, Aug 17, 2003
    #6
    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. Colin JN Breame

    Mutually referential classes

    Colin JN Breame, Jan 21, 2004, in forum: C++
    Replies:
    5
    Views:
    395
    Colin JN Breame
    Jan 22, 2004
  2. Chris Torek

    Re: Mutually referential types

    Chris Torek, Aug 15, 2003, in forum: C Programming
    Replies:
    1
    Views:
    295
    Colin JN Breame
    Aug 16, 2003
  3. Niklas Matthies
    Replies:
    0
    Views:
    822
    Niklas Matthies
    Oct 24, 2006
  4. G Patel

    mutually referential (Pg 140 K&R2)

    G Patel, Apr 23, 2005, in forum: C Programming
    Replies:
    14
    Views:
    652
  5. jrwats
    Replies:
    6
    Views:
    333
    Zachary Turner
    Jan 18, 2009
Loading...

Share This Page