converting from a const char to a char.
Hi,
I need help with converting a const char to a char. That is part of a larger function that is a part of a spell checker, this function is supposed to compare "word" with every word in the dictionary "result" If a match is found, then the word is spelt correctly, and the program returns true... otherwise, it returns false.
Here is my code:
Code:
bool check(const char* word)
{
int r = 0; // might have to be 1 instead of 0
char temp[51];
strncpy(temp,word,51);
char new_word = malloc(strlen(word)+1);
if (new_word) {
strcpy(new_word,word);
printf("%s\n", new_word);
while(r != count);
{
if(strcmp (new_word,result[r]) != 0)
{
r++;
}
else if(strcmp (new_word,result[r]) = 0)
{
return true;
}
}
return false;
}
There are also 2 global variables... here they are:
Code:
int count = 0;
char *result = NULL;
There are 10 errors generated when I try to compile the code. Here is the first one (I believe that fixing this should fix all/most of the other errors):
Code:
dictionary.c:30:10: error: incompatible pointer to integer conversion initializing 'char' with an expression of type
'void *'; [-Werror]
char new_word = malloc(strlen(word)+1);
^ ~~~~~~~~~~~~~~~~~~~~~~
Any help/hints would be GREATLY appreciated!
Thanks,
Josh
I thought all hope was lost..
Hi everyone,
I would like to start by apologising, I should not have expected you to drop everything you were doing to help me. :o
I have also learnt a valuable lesson in scheduling... I left to much till the last minute, and am now paying for that in sleep loss.
With that out of the way, there is actually still hope of me completing this course! I had said that last night was my deadline, but the actual deadline is monday at midnight! The reason I had said that it needed to be done earlier, was because I also have a final project to do, and I though that would need all of the last 2 days. However, I kicked ash this morning, and the final project is just about complete! So, that leaves the rest of today, and all day tomorrow for me to complete the spell checker! If you could take a look at my current problem (below) and give me some help, that would be GREAT!
The problem I am having now is that it gets stuck on the first word it is spell checking... Here is the code for the whole program, the function that the problem seems to be coming from, however, is the very first one, check, so unless you feel like it, I don't think you need to read all the code!
Code:
#include <stdio.h>
#include "dictionary.h"
#include <string.h>
#include <stdlib.h>
#define MAXWORDS 26
#define DLENGTH 46
int count = 0;
char **result = NULL;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
int r = 1; // might have to be 1 instead of 0
char temp[51];
strncpy(temp,word,51);
char *new_word = malloc(strlen(word)+1);
if (new_word)
{
strcpy(new_word,word);
printf("%s\n", new_word);
}
while(r < count);
{
if(strcmp(new_word, result[r]) != 0)
{
r++;
}
else if(strcmp(new_word, result[r]) == 0)
{
return true;
}
}
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
int ch = 0;
FILE *f = fopen("test.txt", "r");
while ((ch = fgetc(f)) != EOF)
{
if (ch == '\n')
count++;
}
size_t total_size = 0;
size_t num_bytes = 0;
char buf[0x1000];
while ((num_bytes = fread(buf, 1, sizeof(buf), f)) > 0) {
char *p = realloc(result, total_size + num_bytes);
if (p == NULL) {
fclose(f);
free(result);
abort(); // whatever, out of memory
}
result = &p;
memcpy(result + total_size, buf, num_bytes);
total_size += num_bytes;
}
//printf("%s\n",result);
//fclose(f);
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
if (count != 0)
{
return count;
}
else
{
return 0;
}
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
return true;
}
When I run the program, it seems to get stuck on this line of code:
Code:
if(strcmp(new_word, result[r]) != 0)
Any Idea what is wrong???
Thanks,
Josh