Thread: typedef and structs

  1. #1
    FOX
    Join Date
    May 2005
    Posts
    188

    typedef and structs

    Is the following code ok? It compiled without any warnings from GCC, but it looks a bit strange.

    Code:
    typedef struct tnode
    {
            int data;
            struct tnode *left;
            struct tnode *right;
    }tnode;
    Since I'm using tnode as both the name of the struct, and for the typedef, there shouldn't be any difference between these two declarations? Shouldn't the typedef conflict with the struct declaration?

    Code:
    tnode *node1;
    struct tnode *node2;

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it looks a bit strange
    It does, doesn't it? But it's well defined and perfectly legal. Tag names and typedef names are in different name spaces, so they won't conflict. However, that doesn't say anything about the advisability of this trick. It can confuse people (as you've discovered), so it should probably be avoided in favor of unique names, or a typedef'd unnamed struct.
    My best code is written with the delete key.

  3. #3
    FOX
    Join Date
    May 2005
    Posts
    188
    How do you create a typedef'd unnamed struct when you need to use pointers to the struct within the struct itself? This works, but again, it looks a bit crazy:
    Code:
    typedef struct
    {
            int data;
            struct tnode *left;
            struct tnode *right;
    }tnode;
    I don't under stand how "struct tnode *left" can work, when there's no struct with the name tnode, only a typedef. Likewise, this works, but I don't understand why (outside the struct):
    Code:
    struct tnode *node1
    This does not work:
    Code:
    typedef struct
    {
            int data;
            tnode *left;
            tnode *right;
    }tnode;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It might compile, but your left/right are not the right type.
    Code:
    typedef struct
    {
      int data;
      struct tnode *left;
      struct tnode *right;
    } tnode;
    
    int main()
    {
      tnode foo, bar;
      foo.left = &bar;  /* warning: assignment from incompatible pointer type */
      return 0;
    }
    > I don't under stand how "struct tnode *left" can work
    For a pointer to a struct, the compiler only needs to know the name of the struct. It doesn't need to know anything about it's contents.

    If you did
    struct tnode node; /* not a pointer */
    Then you'd see whether you had a properly defined struct in place or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    FOX
    Join Date
    May 2005
    Posts
    188
    > For a pointer to a struct, the compiler only needs to know the name of the struct. It doesn't need to know anything about it's contents.

    But in my example, the compiler doesn't have the name of the struct. It only has a typedef of the struct. Is that the same thing?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, the struct tnode is not the same thing as the typedef name tnode.
    The struct which you're typedef'ing is anonymous - it certainly doesn't have an implicit name of tnode.

    Code:
    typedef struct  /* anonymous_tag_name here */
    {
      int data;
      struct bargle *left;
      struct bargle *right;
    } tnode;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    FOX
    Join Date
    May 2005
    Posts
    188
    Code:
    typedef struct  /* anonymous_tag_name here */
    {
      int data;
      struct bargle *left;
      struct bargle *right;
    } tnode;
    
    int main()
    {
      tnode foo, bar;
      foo.left = &bar;  /* warning: assignment from incompatible pointer type */
      return 0;
    }
    So how can you assign a compatible pointer to foo.left/right, when there's no way to create a pointer of the correct type? My mind is running circles right now, because I've seen some code that used this kind of stuff with binary search trees.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like so
    Code:
    typedef struct tnode_tag
    {
      int data;
      struct tnode_tag *left;
      struct tnode_tag *right;
    } tnode;
    
    int main()
    {
      tnode foo, bar;
      foo.left = &bar;
      return 0;
    }
    You can unroll it a little, like so
    Code:
    struct tnode_tag
    {
      int data;
      struct tnode_tag *left;
      struct tnode_tag *right;
    };
    typedef struct tnode_tag tnode;
    But unless you specifically want the struct tagname to create a self-referential structure, there seems little point in mentioning it, if you're typedef'ing it at the same time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    FOX
    Join Date
    May 2005
    Posts
    188
    > But unless you specifically want the struct tagname to create a self-referential structure, there seems little point in mentioning it, if you're typedef'ing it at the same time.

    That's what I was trying to understand. Everything makes sense now. Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display copied data into structs
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 05-02-2006, 02:12 PM
  2. structs
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 04-30-2006, 08:37 PM
  3. accessing variables using structs
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-25-2006, 11:09 AM
  4. structs
    By bazzano in forum C Programming
    Replies: 21
    Last Post: 04-18-2006, 05:17 AM
  5. Help understanding typedef structs
    By difficult.name in forum C Programming
    Replies: 3
    Last Post: 09-22-2004, 12:43 AM