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;

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

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));
  printf("\n**List still empty, add a word**\n");
    printf("\n**You have successfully added a meaning**\n");
printf("\n**Word is not on the list, cannot add meaning**\n");

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:


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

it only prints the last two meanings...

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.