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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:45:32+00:00 2026-06-13T03:45:32+00:00

This is my code for a hash table which stores string values.To use Linear

  • 0

This is my code for a hash table which stores string values.To use Linear probing in my “insert” function, I need to check whether the pointer is NULL at that specific hash value. I Haven’t completed my insert function yet, but I am stuck because when I check for if(the_hash_table[n]==NULL) inside the insert function, it does not enter the branch. Before hashing the value if I print “the_hash_table[1]” it prints “faz”, but after that step when I print it, it prints some weird characters. Where have I gone wrong?

 #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    /*
    creates a hash table of size 10
    */

    char** create_hash_table(){



        char* the_hash_table[10];   // defines a hash table to store strings

        *the_hash_table=malloc(sizeof(char*)*10); // allocates memory in the heap for the hash table
        int i;
        for(i=0;i<10;i++){ // this loop initializes the string pointers to NULL at the starting point of the hash table
            the_hash_table[i]=NULL;
        }
        return &the_hash_table; // returns the address of the hash table to the main memory

    }

    /*
    this is a method to insert a string into the relevant position of the hash table
    */

    void insert(char* the_string,char** the_hash_table){

        printf("%s",the_hash_table[1]);
        int n=hash(the_string);
        printf("%s",the_hash_table[1]);
        if(the_hash_table[n] == NULL)
            the_hash_table[n]=the_string;

    }
  • 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-13T03:45:34+00:00Added an answer on June 13, 2026 at 3:45 am

    You haven’t allocated memory correctly.

    You define an automatic variable, the_hash_table as an array of pointers. You allocate some memory and put the pointer to that memory in your array. You immediately overwrite that pointer (and the other elements of the_hash_table) with null pointers.

    Then you return a pointer to the local array, but the array no longer exists once the function exits. It is always wrong to return a pointer to an automatic variable from the function it’s defined in.

    What you should do is:

    char** create_hash_table(void) {
        char** the_hash_table = malloc(sizeof(*the_hash_table) * 10);
        for (int i = 0; i < 10; ++i) {
            the_hash_table[i] = NULL;
        }
        return the_hash_table;
    }
    

    So, the_hash_table is a local variable that points to the allocated memory. You return its value, which is the address of the allocated memory. Then in main you would free(the_hash_table) not free(*the_hash_table).

    Also, in your hash function there is no point copying the string: just read the characters from the_string[i]. Even if there was a point in copying it, the buffer that you create in order to do so is 1 byte too small, it needs to be strlen(the_string)+1 because the length returned by strlen does not include the 0 byte that terminates the string.

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

Sidebar

Related Questions

This code disabled mouse scroll functionality, when i click on the document. $(document).on(click, function
I'm trying to implement an efficient hash table where collisions are solved using linear
So, let's say I have this code (VB.Net): Sub Main() dim xxx as string
I would like to define a hash table with a swappable hash function. The
This is a hash in which mysql column is related to key of hash
I have a code pattern which translates one integer to another. Just like this:
In R programming language I want to use a hash table. How do I
I am trying to delete all the items in my Hashtable with this code
I have some ruby code like this: my_hash = { key1: value, key2: value
This code below allows me to find the word error in all my files

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.