0

I am working on my dictionary.c right now and am in the process of debugging. When I try to compile my code, the error message 'char [46]' is not assignable showed up for my malloc function. I tried looking trough the code myself, but I really don't know what I did wrong. Can someone please help and see if they can find what is wrong with my code? :(

Thanks,

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"

#define DICTIONARY "/home/cs50/pset5/dictionaries/large"
#define hashsize 5000

/**
* Returns true if word is in dictionary else false.
*/


 typedef struct node

{
char word [LENGTH + 1];
struct node * next;
} 

node; 

node* hashvalue[hashsize];

int hash (const char* word)
{
int hashcount = 0;

for (int a = 0, n = strlen (word); a<n; a++)
{
    hashcount = word[a] + hashcount;
}
return hashcount % hashsize;
}

int word_count = 0;

bool check(const char* word)
{
// TODO

char lowercase[LENGTH +1];

for ( int i = 0; i < strlen (word)  ; i++)
{

char temp = tolower (word[i]);

lowercase[i] = temp;

}

lowercase[strlen(word)] = '\0';

int index = hash (lowercase);

node * extract  = hashvalue [index];

// go through all linked list to check
while (extract != NULL)
{
    if(!strcmp ( extract -> word, lowercase))

    {
    return true;
    }

    else

    {
    return false;
    }
}

word_count++;

return false;

}

/**
* Loads dictionary into memory.  Returns true if successful else false.
*/

int n_count = 0;

bool load(const char* dictionary)
{
// TODO
FILE* dic_ptr = fopen(dictionary, "r");
char scanned_word[LENGTH+1];

if(dic_ptr == NULL)

{
return false;
}


//scanning the dictionary until the end

int index=0;

while (fscanf (dic_ptr, "%s\n",scanned_word) != EOF )

{

node * node_1 = malloc (sizeof (node));

node_1 -> word = malloc (strlen (scanned_word)+1);

strcpy ( node_1 -> word, scanned_word );

index = hash (scanned_word);

n_count++; 

if (hashvalue[index] ==NULL)
{
    hashvalue[index] = node_1 ;

}

else 
{
    node_1 -> next = hashvalue [index];
    hashvalue[index] = node_1;
}
}

fclose (dic_ptr);

return true;
}

/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
// TODO 
return n_count;
return word_count;
}

/**
* Unloads dictionary from memory.  Returns true if successful else false.
*/
bool unload(void)
{
// TODO
for (int c = 0; c < hashsize; c++)
{

node * extract = hashvalue [c];

while (extract != NULL)
{
    node* next = extract -> next;

    free(extract);

    extract = next;
    }
    }

return true;
}
2

I bet your code is failing with this line:

node_1 -> word = malloc (strlen (scanned_word)+1);

node_1->word is a 46 char array (LENGTH + 1 = 45 + 1 = 46), not a pointer. You are trying to assign the pointer that is returned by malloc to the array word.

You don't need this line. The previous line allocates the 46 char array for word inside the structure when it allocates the space for the entire node type.

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

3
  • OMG IT WORKED!!!!!! thanks for helping me again Cliff! – Leon Dec 13 '15 at 18:04
  • Glad it worked. Now, could you please accept the answer by clicking on the check mark? Otherwise, it will stay in the unanswered question queue forever. thanks. – Cliff B Dec 13 '15 at 18:28
  • Done!!!!!!!! :) – Leon Dec 13 '15 at 18:29

You must log in to answer this question.

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