0

My load() function updates a global array initialized to NULL. check() thereafter converts word to lowercase and stores it in a buffer. Most elements in the array store NULL, however sometimes a real memory address is found and the condition for the while loop, as well as the if condition succeeds and true; gets returned.

The relevant two functions are below together with global variables and arrays.

// Global variables and arrays for simple access
char buffer[LENGTH+1]; // Declares the buffer to store one line or one word in total
int wordcount = 0;
int hash_index = 0;

node* head_array[HASHTABLE_SIZE] = {NULL}; // Stores pointer to node datatype
node* head_node;  // Will always be the most recent node, and first in the list
FILE* dict_file;


 /**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    // Open dictionary file, and error-check
    dict_file = fopen(dictionary, "r");
    if (dict_file == NULL)
        return false;


    while (fgets(buffer, LENGTH+1, dict_file) != NULL) {

    */ REDACTED TO COMPLY WITH ACADEMIC HONESTY /*

    return true;
}

Some messing around with gdb says that head_array contains:

{0x0, 0x0, 0xebd750, 0x0, 0x0, 0x0, 0xebef10, 0x0, 0x0, 0x0, 0xebe510, 
  0x0, 0x0, 0x0, 0xebed10, 0x0, 0x0, 0x0, 0xebee50, 0x0, 0x0, 0x0, 0xebded0, 
  0x0, 0x0, 0x0, 0xebe890, 0x0, 0x0, 0x0, 0xebeb50, 0x0, 0x0, 0x0, 0xebe550, 
  0x0, 0x0, 0x0, 0xebe650, 0x0, 0x0, 0x0, 0xebebd0, 0x0, 0x0, 0x0, 0xebecd0, 
  0x0, 0x0, 0x0, 0xebe850, 0x0, 0x0, 0x0, 0xebed50, 0x0, 0x0, 0x0, 0xebeed0, 
  0x0, 0x0, 0x0, 0xebe110, 0x0, 0x0, 0x0, 0xebee90, 0x0, 0x0, 0x0, 0xebec10, 
  0x0, 0x0, 0x0, 0xebee10, 0x0, 0x0, 0x0, 0xebe410, 0x0, 0x0, 0x0, 0xebe1d0, 
  0x0, 0x0, 0x0, 0xebe610, 0x0, 0x0, 0x0, 0xebe010, 0xbd1a90, 0x0, 0x0, 
  0xebedd0, 0x0, 0x0, 0x0, 0xebec50, 0x0}

1 Answer 1

1

Don't forget that fgets will read in the newline character as the last char of each word. Because of this, you are storing that newline char in your head_node->word. But in check, the words don't have \n at the end, so all words will end up misspelled.

You can see this by adding this debugging line to your load function, just after you've done the strcpy.

        printf("strlen of %s is %lu\n", head_node->word, strlen(head_node->word));

Perhaps consider using fscanf instead:

   while (fscanf(dict_file, "%s", buffer) > 0){

fscanf, using the %s, will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab). So that way, the newline is not put into the buffer.

You must log in to answer this question.

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