1

Back with more questions about my code.

1) fscanf: When using fscanf do we need to iterate through what it is writing to the buffer to check for the null signifier so we can jump in and strcpy that buffer into our new node?

2) char arrays and strcpy: I created a buffer array of 46 values. Since we know the largest possible word will be 45+Null signifier. Does strcpy automatically detect the NULL signifier and stop copying at that point? Also do I need to free my char array after every single strcpy or will it just get overwritten by the next iteration of fscanf?

3) hash values: From what I understand the hash function will return the index location of whatever index my string belongs to. Then I need to take the address contained within that index location, forward it to my node *next pointer, and then reset that index locations pointer to my current node.

Figuring out how to store the pointer stored within the index returned by the hash function and then pass it to my node* next and then re-allocate that node as the head is where I think I am getting tripped up.

I think I am starting to understand it, but it's very difficult to know if this is all just absolute garbage or not since I am not really sure how to even verify if what I have written so far is accurate.

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO
    // Open Dictionary File
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        printf("Could not open file");
        return false;
    }
    
    // Create a buffer array with 46 values
    char buffer[45];
    
    while(fscanf(dict, "%s" , buffer) != EOF){
        // Iterate through string to know when to stop copying with fscanf
        for(int i = 0; i < 45; i++){
            // When we find a null character we know the word is completely read into buffer
            if(buffer[i] == '\0'){
                // Malloc for a new node
                node *node = malloc(sizeof(node));
                
                    if(node == NULL){
                        printf("Could not allocate memory for *node");
                        return false;
                        
                    }
                    // Copy the contents of buffer into the new node
                    strcpy(node->word, buffer);
                    
                    // Get the hash value of the node
                    
                    ????? = hash(node->word);
                    
                    
            }
        }
    }
    return false;
}

Thanks for all your time and effort.

1 Answer 1

1
  1. When using fscanf do we need to iterate through what it is writing to the buffer to check for the null signifier so we can jump in and strcpy that buffer into our new node?

No. fscanf will just stop when it hits the null terminator. It also copies the null terminator.

  1. Does strcpy automatically detect the NULL signifier and stop copying at that point?

Yes. It also copies the null terminator.

Also do I need to free my char array after every single strcpy or will it just get overwritten by the next iteration of fscanf?

No. You never have to free stack allocated variables. You only free memory allocated with malloc.

And your code does not allocate 46 characters char buffer[45]; You're missing 1 for the null terminator.

  1. From what I understand the hash function will return the index location of whatever index my string belongs to.

Good so far.

Then I need to take the address contained within that index location, forward it to my node *next pointer, and then reset that index locations pointer to my current node.

That sounds about right.

Figuring out how to store the pointer stored within the index returned by the hash function and then pass it to my node* next and then re-allocate that node as the head is where I think I am getting tripped up.

This isn't quite right. You don't need to allocate or re-allocate anything more. You already allocated space for a node. Maybe you didn't mean allocate, but assign?

I think I am starting to understand it, but it's very difficult to know if this is all just absolute garbage or not since I am not really sure how to even verify if what I have written so far is accurate.

Iterating over the characters and checking for a null terminator in buffer is a complete waste of time, but other than that, it looks good. You're just missing some pieces.

I'm just gonna show some code. I don't think these implementation details count as spoilers.

// Get the hash value of the node
????? = hash(node->word);
int index = hash(node->word);

hash presumably returns an int. So we just store the index in an int variable.

You can now set n->next to the correct location in table. And after that set table[location] to n. And you're pretty much done. You just need to return true at the end instead of false.

6
  • "Iterating over the characters and checking for a null terminator in buffer is a complete waste of time, but other than that, it looks good. You're just missing some pieces." - The reason for this being that fscanf already does that? "hash presumably returns an int. So we just store the index in an int variable." - The location of the index is an int? So it would be ideally between 0-25 and then I can reference it in my *table[index] location? or 0-675 if I decide to do the first two letters of each word and use a slightly larger hash table. Thanks for taking the time to help me.
    – Multiplify
    Commented Apr 25, 2021 at 19:36
  • 1
    @Multiplify Yes en.cppreference.com/w/c/io/fscanf > "The location of the index is an int?" I'm a bit confused by that. What did you think it was? > "So it would be ideally between 0-25 and then I can reference it in my *table[index] location? or 0-675 if I decide to do the first two letters of each word and use a slightly larger hash table." Well that depends entirely on your hash function. Which I haven't seen. Commented Apr 25, 2021 at 19:44
  • Honestly I am not completely sure. After you've pointed out that the index is an int it made more sense though. So it just gives me the element within my array that I am connecting the node to. But finding the address that element is pointing at and pointing my node->next at it too is where I kind of get lost. Still struggling to conceptualize how all of this fits together.
    – Multiplify
    Commented Apr 25, 2021 at 19:56
  • @Multiplify "But finding the address that element is pointing at and pointing my node->next at it too is where I kind of get lost." node->next = table[index]; Doesn't have to be more complicated than that. Commented Apr 26, 2021 at 5:22
  • But wont that point my node at the index rather than pointing the index at my node? Shouldn't the pointer value of table[index] be the most recently added node.
    – Multiplify
    Commented Apr 26, 2021 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .