Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:34:26+00:00 2026-06-07T20:34:26+00:00

Disclaimer: This is a homework problem. As should be evident, I am trying to

  • 0

Disclaimer: This is a homework problem. As should be evident, I am trying to solve it myself. I seem to have hit a problem I am unable to figure out, so some help would be appreciated.

I am required to hash a set of words, and insert it into an array of linked lists. So, if 3 different words have the hash 38, at array[38], I need to have a linked list with the 3 words.

I am using this struct

struct Word {
    char* word;
    struct Word* next;
};

After I hash, I insert it into the array like this:

struct Word* table[1000];   // array of linked-lists

char word[128];
while(fgets(word, sizeof(word), dict) != NULL) {
    hashValue = hashWord(word, strlen(word));        

    struct Word *newWord = malloc(sizeof(struct Word));
    newWord->word = word;           

    if (table[hashValue] == NULL) {
        table[hashValue] = newWord;             
    } else {   // at index hashValue, table already contains at least one element
        // insert new word as first element of linked-list
        newWord->next = table[hashValue];
        table[hashValue] = newWord;         
    }

}

I know there are about 5 words that have a hash of 38, but when I print them, I get the same word 5 times:

struct Word* foo = table[38];

while (foo->next != NULL) {
    printf("%s", foo->word); // prints the same word every time
    foo = foo->next;
}

It seems I am overwriting my linked list at some point, but I cannot figure out where.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:34:29+00:00Added an answer on June 7, 2026 at 8:34 pm
    while(fgets(word, sizeof(word), dict) != NULL) {
        ...
        newWord->word = word;           
    

    The problem is that you’re replacing the contents of word, which is the same pointer stored in every newWord.

    You’re allocating a new Word structure on the heap each time with this line:

    struct Word *newWord = malloc(sizeof(struct Word));
    

    But this only allocates memory for the Word structure itself; the Word structure includes a char *word—i.e. a pointer to a character (or NUL-terminated string, in this case), but it doesn’t actually include any space for the string itself.

    You need to explicitly allocate memory for the string, now. Try this:

    newWord->word = strdup(word);
    

    This will put a copy of word into each Word structure. This copy won’t be overwritten when fgets() next gets called in your while loop. Remember that the stack-allocated character array:

    char word[128];
    

    is only valid while this function is executing; you must allocate something on the heap (with malloc(), or something that uses it, like strdup()) if you want it to live beyond the function call.

    When you’re finished, don’t forget to free() the char *words before freeing each Word*.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: this is homework. I have a SAS dataset of: ID VAL YEAR I
Disclaimer: this is a (frustrating) homework related problem. I'm having odd results when I
Disclaimer: this was a homework problem. The deadline has passed now, so discussions can
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
(Disclaimer: I realize this is a massive wall of text, but I have done
Full disclaimer: this is not really a homework, but I tagged it as such
Disclaimer: this question is purely informational and does not represent an actual problem I'm
DISCLAIMER : this is the classic case of .NET GUI trying to work his
Disclaimer: this question is directly related to my programming homework. My C++ assignment consists
( Disclaimer: This question is not specific to ASP.NET) I have a control which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.