I fixed the error shown in the terminal for line 24, but when I compile my code it keeps popping up again [even though the line it's referencing does not even exist anymore].
dictionary.c:24:15: error: implicitly declaring library function 'malloc' with type 'void *(unsigned long)' [-Werror,-Wimplicit-function-declaration]
node *node1 = malloc(sizeof(node));
^
dictionary.c:24:15: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
dictionary.c:24:15: error: initializer element is not a compile-time constant
node *node1 = malloc(sizeof(node));
^~~~~~~~~~~~~~~~~~~~
dictionary.c:25:15: error: initializer element is not a compile-time constant
node *node2 = malloc(sizeof(node));
^~~~~~~~~~~~~~~~~~~~
Here is my actual code [I'm still working on it, just not sure how to approach this]:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include "dictionary.h"
// Represents number of buckets in a hash table
#define N 26
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Represents a hash table
node *hashtable[N];
//THIS IS LINE 24!!!!!
// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
// TODO
//make a new node for each word
node *new_node = malloc(sizeof(node));
//I NEED A HASH FUNCTION HERE
//add word into node...
strcpy(new_node -> word, word);
if (new_node == NULL)
{
upload();
return false;
}
}
// Close dictionary
fclose(file);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
// TODO
//if file is loaded
if(file)
{
//iterate through all ch of file
while ((ch=getc(file)) != EOF)
{
// // Increment character count if NOT new line or space
// if (ch != ' ' && ch != '\n')
// { ++charcount; }
// Increment word count if new line or space character
if (ch == ' ' || ch == '\n')
{
++wordcount;
}
}
if(wordcount > 0)
{
//// Increment line count if new line character
// if (ch == '\n') { ++linecount; }
return wordcountl;
}
//if no words found
else
{
printf("no words found in dictionary");
return 0;
}
}
printf("no words found in dictionary");
}
return 0;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// TODO
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
return false;
}
I no longer even have this on line 24:
node *node1 = malloc(sizeof(node));
I've tried resetting the site [reloading], closing files... Why does it keep showing up, and can I remedy this?