#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    What's wrong with the syntax?


    Hi guys.

    i'm trying to define the following:

    Code:
    typedef struct
    {
    	professor* current;
    	list* next;
    }list;
    but i'm getting a compilation error: "unknown type name 'list'".
    how come?

    how else can i define a nested struct of the same type?

    thanks in advanced!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    There may be other ways. I actually had to look this one up, since my schooling in linked lists was in my pre-C days, and since C++'s approach to a similar problem is slightly different syntactically.

    Code:
    typedef struct _list
    {
    	professor* current;
    	struct _list* next;
    }list;
    For the field, next, the compiler has to already know about list, but it doesn't yet. However, it does know about struct _list. And since in subsequent code and declarations list will already be known to be struct _list, next should be compatible with pointers declared as list* .

    Hopefully there are better solutions, but I just can't think of any.
    Last edited by dwise1_aol; February 4th, 2013 at 04:25 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    yup. thanks man.
    now it compiles without any errors.

    only now, Eclipse warns me every time i try to assign from "struct list" type to "list" type and vise versa, since it sees them as different types ("assignment from incompatible pointer type").
    nothing i can't live with, though...
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    A preprocessor alternative (much despised by some) I use:
    Code:
    #define NODE struct node_structure
    NODE {
      void*data;
      NODE*next;
    };
    
    NODE*stack;
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Originally Posted by so.very.tired
    yup. thanks man.
    now it compiles without any errors.

    only now, Eclipse warns me every time i try to assign from "struct list" type to "list" type and vise versa, since it sees them as different types ("assignment from incompatible pointer type").
    nothing i can't live with, though...
    Did you notice that struct _list has a leading underscore in front of it, whereas the typedef doesn't. Just declare all your variables to be of type list instead of struct _list and you should have no warnings.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    An alternative is to use C++ compilation where the typedef nonsense is unnecessary:

    In C++:
    Code:
    struct list
    {
    	professor* current;
    	list* next;
    } ;
    Defines a type list that does not require the struct keyword to be used everywhere it is referenced.

    That said the C++ standard library includes a list container class that would probably make this struct redundant in any case.

IMN logo majestic logo threadwatch logo seochat tools logo