Re: Mutually referential types

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

  1. Chris Torek

    Chris Torek Guest

    In article <>
    Colin JN Breame <> writes:
    >Heres the problem:
    >
    >typedef struct {
    > B *pB;
    >} A;
    >
    >typedef struct {
    > A *pA;
    >} B;
    >
    >This wont compile as B is not defined until after A.


    "Best" method (personal opinion, others disagree): do not use typedefs.

    Next-best method:

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

    If you think of the word "struct" as standing for "type" (which it
    does!), then "struct A" means "type A", and you always know that
    "type X" is type X, and never some other X.

    In particular:

    typedef int x;
    void f(x x) {
    x = 3;
    }

    is actually valid C code. The inner "x" shadows the outer "x" so
    that inside f(), "x" is an ordinary variable of type int. Ultimately,
    what this means is that code using "struct" keywords is far easier
    to read than code using identifiers that have been typedef'd. This
    "typedefs make code hard to read" problem is often papered over
    (some claim "fixed" :) ) by inventing funky spelling conventions
    for all typedefs -- e.g.:

    typedef struct foo foo_t;
    or typedef struct foo Foo;

    so that whenever the reader sees "foo_t" or "Foo", it is reasonably
    clear that this is a typedef'ed identifier.
    --
    In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://67.40.109.61/torek/index.html (for the moment)
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Aug 15, 2003
    #1
    1. Advertising

  2. > "Best" method (personal opinion, others disagree): do not use typedefs.
    >
    > Next-best method:
    >
    > typedef struct A A;
    > typedef struct B B;
    > struct A {
    > B *pB;
    > };
    > struct B {
    > A *pA;
    > };
    >
    > If you think of the word "struct" as standing for "type" (which it
    > does!), then "struct A" means "type A", and you always know that
    > "type X" is type X, and never some other X.


    Chris,

    Thanks for your reply, my mind is much clearer now.
     
    Colin JN Breame, Aug 16, 2003
    #2
    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:
    407
    Colin JN Breame
    Jan 22, 2004
  2. Eric

    Re: Mutually referential types

    Eric, Aug 15, 2003, in forum: C Programming
    Replies:
    5
    Views:
    354
    Mark Haigh
    Aug 17, 2003
  3. Niklas Matthies
    Replies:
    0
    Views:
    839
    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:
    665
  5. jrwats
    Replies:
    6
    Views:
    347
    Zachary Turner
    Jan 18, 2009
Loading...

Share This Page