0

As the title suggests I having a bit of trouble with my if and else down at the bottom of my code. Errors are arising that say incompatible pointer to integer conversion. I am a bit confused as to whether to the problem lies with the code above the issues, or the actual code in the if and else statements. I am still a bit fuzzy on some of the dereferencing concepts and such.

 /**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
 // hash function
int hash_fxn (char* key)
{
// hash on 1st letter
int hash = toupper(key[0]) - 'A';
return hash % 26;
}

bool load(const char* dictionary)
{
// TODO
// word counter
int wordcount = 0;

// open dictionary
FILE* dict = fopen(dictionary, "r");

if (dict == NULL)
{
    printf("Dictionary could not be loaded\n");
    return false;
}

// create the base array of the alphabet plus a space for apos.
int alphabet[26];
for (int i = 0; i < 27; i++)
    alphabet[i] = 0;

// create a node for a separate chaining hash table
typedef struct node
{
    // array to store word
    char word[LENGTH + 1];

    //create a pointer to the next node
    struct node* next;
}
node;

// while the dictionary is not finished
while (!feof(dict))
{
    // create space for new_node
    node* new_node = malloc(sizeof(node));

    // read word from dictionary and place it in new node
    fscanf(dict, "%s", new_node->word);

    // add to wordcount
    wordcount++;

    // make the new_node not point to anything
    new_node->next = NULL;

    // generate hash value
    int hash_value = hash_fxn(new_node->word);

    // if there is nothing in the hash bin
    if (alphabet[hash_value] == 0)
        alphabet[hash_value] = new_node;
    // if something is in bin insert at the head of the bin
    else
    {
        new_node->next = &alphabet[hash_value];
        alphabet[hash_value] = new_node;
    }
}

return true;

}

1

The problem lies in the following:

    alphabet[hash_value] = new_node;

The alphabet[] array is an array of integers. 'new_node' is a pointer to a node. This line of code is trying to stuff a memory address (the contents of new_node) into an int (alphabet[hash_value]).

Your underlying problem is that you defined the alphabet array as type int when you should have defined it as an array of pointers to nodes, just like you did with new_node. Now, once you do this, your test of == 0 is going to present problems. It will need to be changed to == null instead.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you, Cliff. :)
    – peachykeen
    Dec 5 '15 at 3:18

You must log in to answer this question.

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