Jump to content
xisto Community
Sign in to follow this  
beeseven

How Can I Have A Structure Contain One Of Itself? (c)

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

struct listnode{   int data;   struct list *nextnodelink; }

You will have to create each node individually and link them in your code.

Share this post


Link to post
Share on other sites

you might want to try checking an error reference on your system to find out a specific meaning of the error.also, this is a circular linked list. This means that the last item links back to the first. This could have something to do with your problem, especially if extra memory is being allocated along the way. try not linking the last item to the first and see what happens.you might also have some luck if you email the guy who wrote it. you could probably find his email somewhere on his site.good luck mate.

Share this post


Link to post
Share on other sites

Alright, now I've got this: http://forums.xisto.com/no_longer_exists/. It compiles fine, but if I try to run it, it hits an error. If I run it on the Linux server, it says "segmentation fault." Any ideas?


Just change

	listnode n1;	setval(n1, 1);	listnode n2;	setval(n2, 2);	listnode n3;	setval(n3, 3);

to

	listnode n1=malloc(sizeof(struct alistnode));	setval(n1, 1);	listnode n2=malloc(sizeof(struct alistnode));	setval(n2, 2);	listnode n3=malloc(sizeof(struct alistnode));	setval(n3, 3);

It works. There was a memory allocation problem in this code. n1, n2, n3 were used without memory being allocated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.