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 6804923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:31:30+00:00 2026-05-26T19:31:30+00:00

This is a lot of code but I’m at ends here trying to figure

  • 0

This is a lot of code but I’m at ends here trying to figure out why its not working.

This is the code I’m running:

struct dict {
  struct word **tbl;
  int (*hash_fcn)(struct word*);
  void (*add_fcn)(struct word*);
  void (*remove_fcn)(struct word*);
  void (*toString_fcn)(struct word*);
};

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

this is part of the main function:

  hashtbl=malloc(sizeof(struct dict));
  //hashtbl->tbl=malloc(sizeof(struct word*)*256);
  int i;
  for(i=0;i<256;i++)
  {
    hashtbl->tbl[i] = malloc(sizeof(struct word));
    hashtbl->tbl[i] = NULL;
    //hashtbl->tbl[i]=NULL;
  }
  hashtbl->add_fcn=&add_word;
  hashtbl->remove_fcn=&remove_word;
  hashtbl->hash_fcn=&hash_function;
  hashtbl->toString_fcn=&toString;

  FILE *p;
  p = fopen("a.txt", "r");
  if(p == NULL) { printf("ERROR!\n"); return 0; }
  char line[MAXBYTES];
  while(fgets(line, MAXBYTES, p))
  {
      char *token = strtok(line, " ");
      while(token != NULL)
     {
        struct word *words = malloc(sizeof(struct word));
        words->s = token;
        words->next=NULL;
        trim(words);
        hashtbl->add_fcn(words);
        token = strtok(NULL, " ");

        printf("....\n");
        hashtbl->toString_fcn(NULL);
        printf("....\n\n");

      }
      free(token);
  }

Here are two functions used (to String just prints it out)

void add_word(struct word *insert)
{
  if(strcmp(insert->s, "\n") == 0) {return;}

  int hash = hashtbl->hash_fcn(insert);

  if(hash==0) { return; }
  struct word *word = hashtbl->tbl[hash];
  if(word==NULL)
  {
    struct word *newword = malloc(sizeof(struct word));
    newword->s=insert->s;
    newword->next=NULL;
    hashtbl->tbl[hash]=newword;

    printf("() %d %s \n", hash, hashtbl->tbl[hash]->s);
  }
  else
  {
    struct word *tp = word;
    while(word->next != NULL)
      word=word->next;
    struct word *newword = malloc(sizeof(struct word));
    newword->s=insert->s;
    newword->next=NULL;
    word->next=newword;
    hashtbl->tbl[hash]=tp;
  }
}

int hash_function(struct word *string)
{
  char *firstletter = string->s;
  char c = *firstletter;
  int ascii = (int)c;
  return ascii;
}

a.txt is

cat
dog
mouse

and it prints out the following:

() 99 cat 
....
99 : cat 
....

() 100 dog 
....
99 : dog 
100 : dog 
....

() 109 mouse 
....
99 : mouse 
100 : mouse 
109 : mouse 
....

it should be printing out

99:cat 
100:dog
109:mouse

thanks

  • 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-05-26T19:31:31+00:00Added an answer on May 26, 2026 at 7:31 pm

    You have many problems here.

    First:

    hashtbl=malloc(sizeof(struct dict));
    //hashtbl->tbl=malloc(sizeof(struct word*)*256);
    

    You need to uncomment that line, since without it you don’t allocate the array of struct pointers in hashtbl->tbl, and leave it uninitialized. It’ll probably cause a segfault as is.

    Next:

    for(i=0;i<256;i++)
    {
      hashtbl->tbl[i] = malloc(sizeof(struct word));
      hashtbl->tbl[i] = NULL;
      //hashtbl->tbl[i]=NULL;
    }
    

    This is leaking memory. You’re allocating a struct word to each element of hashtbl->tbl, but then overwriting the pointer with NULL. So you leak all the memory you allocated, and leave each array element set to NULL. Get rid of the malloc() and just set them to NULL, since you’ll allocate the structs later when you actually add a word.

    In this part:

    hashtbl->add_fcn=&add_word;
    hashtbl->remove_fcn=&remove_word;
    hashtbl->hash_fcn=&hash_function;
    hashtbl->toString_fcn=&toString;
    

    …the & are not needed — the function names without parentheses are good enough. As long as the parentheses are omitted, it will be interpreted as the address of the function and not a function call.

    And then here:

        char *token = strtok(line, " ");
        while(token != NULL)
       {
          struct word *words = malloc(sizeof(struct word));
          words->s = token;
    

    …the char * returned by strtok() typically points within the string you’re processing, i.e. within the buffer you read lines into. It will not remain intact while you continue processing the file. You need to make a copy of the string, not just save the pointer.

        free(token);
    

    …are you sure you should free this?

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

Sidebar

Related Questions

This is the code but it's not working on IE8 & 7 (IE9 ,
There are a lot of posts on here about this, I'm using this code
I have tried a lot on this code but it keeps messing up. The
This is a common error, I'm sure, but I can't figure out why I'm
Note there's a lot of code posted below but its mostly all the same,
I have read through a lot of code but I do not understand how
I see a lot of code like this: function Base() {} function Sub() {}
I find myself writing code that looks like this a lot: set<int> affected_items; while
In my project I have a lot of code like this: int[][] a =
I see a lot of example code for C# classes that does this: public

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.