I do have 2 structures here. main linked list is the word. each node of the word list do have a meaning

typedef struct node{
  char word[20];
  struct node2 *meaning;
  struct node *next;
}word;

typedef struct node2{
  char meaning[100];
  struct node2 *next;
}means;

My problem now is I can't add multiple meanings. I can just add 1 meaning and whenever i try to add, it overwrites the previous meaning instead., how can i do this? this is how i add meaning

word *tmp2=(*head);
means *tmp3;
tmp3=(means *)malloc(sizeof(means));
if(tmp2==NULL){
  printf("\n**List still empty, add a word**\n");
  return;
}
do{
  if(strcmp(tmp2->word,ins)==0){
    tmp2->meaning=tmp3;
    strcpy(tmp3->meaning,mea);
    tmp3->next=NULL;
    printf("\n**You have successfully added a meaning**\n");
    return;
  }
  tmp2=tmp2->next;
}while(tmp2!=NULL);
printf("\n**Word is not on the list, cannot add meaning**\n");
return;

Recommended Answers

All 5 Replies

You use some variables that aren't declared in the code. (e.g. mallocing with the size of "means" but later copying "mea" to that buffer. I will just assume this is correct, as I have no other choice)

The essence of your problem is the following line:

tmp2->meaning=tmp3;

Once you've found your word you just overwrite it's meaning. (Note that you also do not free() the possibly existing meaning in this case) Like with the words, you should go through the linked list of meanings.

Something like the following:

node2** insert = &(tmp2->meaning);

while ((*insert) != NULL)
{
    insert = &((*insert)->next);
}

// Insert now points at the memory address you should place your pointer
// e.g:
*insert = tmp3;

Quick dry-code, so consider it's working mainly. If you don't like pointers to pointers you could also do something like the following which might be easier to read:

node2* insert;

if (tmp2->meaning == NULL)
{
    tmp2->meaning=tmp3;
}
else
{
    insert = tmp2->meaning;

    while (insert->next != NULL)
    {
        insert = insert->next;
    }

    insert->next = tmp3;
}

So basically apply what you're doing to words to the meanings..

@gonbe
it only prints the last two meanings...

@gonbe
it only prints the last two meanings...

Be more specific and provide some context. I can't do anything with this.
Posting a minimal yet complete code sample helps too.

if you want to check i can give you the whole code

if you want to check i can give you the whole code

I don't want to check the whole code. Appearently something is going wrong it it, so isolate the problem to a small representative section of code and show me that so I can fix it or atleast provide advance on how to fix it or what direction to think in.

Your original problem was "meanings" getting overwritten, I've answered that with how to fix it but you reply with a seemingly completely unrelated problem.

It might be a good idea to (re-)educate yourself about linked lists. Your first problem shouldn't have been a problem in the first place as you (kind of) apply the solution on the 'outer' linked list. A linked list is nothing more than a chain of structures referencing to eachother by pointers. You can add or remove individual structs from it in an intuitive way. The chain ends when it is no longer connected to another section. (programmaticaly meaning 'next' is NULL)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.