The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
What's wrong with the syntax?
Discuss What's wrong with the syntax? in the C Programming forum on Dev Shed. What's wrong with the syntax? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 4th, 2013, 04:06 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 112
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
|
|
|
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!
|

February 4th, 2013, 04:21 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

February 4th, 2013, 04:31 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 112
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
|
|
|
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...
|

February 4th, 2013, 05:12 PM
|
 |
Contributing User
|
|
|
|
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!
|

February 4th, 2013, 05:33 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Quote: | 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
|

February 5th, 2013, 05:11 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|