Jul 20, 2008

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

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

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

beeseven
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.

Reply

kvkv
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

beeseven
Alright, now I've got this: http://tjhsst.edu/~tsmilack/c/listnodes.c. 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?

Reply

switch
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.

Reply

kvkv
QUOTE(beeseven @ Feb 5 2006, 04:32 PM) *

Alright, now I've got this: http://tjhsst.edu/~tsmilack/c/listnodes.c. 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

CODE

    listnode n1;
    setval(n1, 1);

    listnode n2;
    setval(n2, 2);

    listnode n3;
    setval(n3, 3);


to

CODE

    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.

Reply

beeseven
Awesome! It works! Thanks, kvkv, because I think I also understand malloc now.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : structure


    Looking for structure, c

Searching Video's for structure, c
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE