Originally posted by bobthebuilder20
hello quzah,
my structure is as follows:
struct list
{
int rank;
char *string;
struct list *prev;
struct list *prev_rank;
struct list *next;
struct list *next_rank;
} LIST;
prototype:
LIST *all_mem(int, char *, LIST **head);
Cheers.
Previously, you had done the following:
Code:
newmember->rank = (int)malloc(sizeof(num));
newmember->rank = num;
Since in your function, 'rank' is not a pointer, but is a standard member of said structure, you do not allocate space for it. You only allocate space for items you're dynamicly creating. That is to say, if you have a pointer, and you want it to point to something that hasn't been created, you allocate for it.
Since every single list structure you create has an integer set aside called rank, you do not allocate space for it. The structure does that automaticly since rank is not a pointer.
Thus, all you have to do is:
Code:
newmember->rank = num;
For the second answer, I've already given it. Since you're using a pointer to a pointer to update the head of your list, all you do is, in this order:
Code:
newmember->next = *listhead;
*listhead = newmember;
What this does is first, it makes the next member of the new node point to whatever node is the top of the list.
Second, it sets the top of the list to be the new node.
You have to do those two steps in that order. If you don't, this will happen:
Code:
*listhead = newmember;
newmember->next = *list;
This makes the top of the list be the new node. Since you didn't keep track of what used to be the top first, the old list is lost.
Second, it then makes the next member of the new node point back at itself.
That is what you were doing. That is why your list was disapearing.
Quzah.