QUOTE(beeseven @ Feb 4 2006, 09:09 PM)

I'm just making random things as I'm learning C, so I decided to try linked lists/nodes for my practice with structures. I have the nodes keeping an integer value and a link to another node, but I can't figure out how to make the compiler accept the structure definition containing a reference to itself. I've looked on Google extensively, but I can't find anything that works.
Actually, you can't create a structure which contains a member of type itself. This is because this kind of structure, when initialized, will start creating a structure inside a structure inside a structure and so on till all the memory is exhausted and crashes. That is why compiler does not allow you to create a member of same type.
But there is a solution. You can have a pointer to the same structure as member. It means that when an element of this struct (or class in c++) is created, only a pointer is created and no memory allocated, so it is not recursive.
Here is a sample code
CODE
struct listnode
{
int data;
struct list *nextnodelink;
}
You will have to create each node individually and link them in your code.
Reply