why isn't doing what I want it to do?
Simple...Array of different words, Initial array size is 4. Upon reaching the limit, it shoudl dynamically expand. MY CODE ISN"T DOING THIS. Also, I am trying to qsort it after each addition...well IT ISN:T DOING THSI ALSO... Why? What's wrong and what should I look out for? Here is the code: THANK YOU SOOO MUCH !
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SPELL_OKAY 0 /* success */
#define SPELL_FAILURE -1 /* general, unspecified failure */
#define SPELL_NOMEMORY 1 /* could not allocate more memory */
#define SPELL_NOTFOUND 2 /* could not find a file */
#define SPELL_TOOLONG 3 /* a word is too long */
#define SPELL_INCORRECT 4 /* a word is incorrect */
#define MAXLENGTH 255
char *dictionary;
char *temp;
int arraySize = 4;
int count = 0;
char name[] = "kar";
char *name2 = name;
char name3[] = "A";
char *name33 = name3;
int check_word(char *word);
int max_word_len(void);
int add_word(char *word);
/*int charcompare(const void *p1, const void *p2);*/
int main()
{ dictionary = (char *)malloc(arraySize*sizeof(char));
add_word(name2);
add_word(name2);
add_word(name2);
add_word(name33);
printf("%s\n", dictionary[3]);
}
int charcompare(const void* p1, const void * p2)
{
char * i = *((char*)p1);
char * j = *((char*)p2);
if (i > j)
return (1);
if (i < j)
return (-1);
return (0);
}
int max_word_length(void)
{ return MAXLENGTH;
}
int add_word(char *word)
{ int result;
if( (result= check_word(word)) != SPELL_OKAY)
return result;
else
{ if (count >= arraySize)
{ printf("DEBUG: reallocating dictionary ... \n") ;
arraySize = 2 * arraySize;
temp = realloc(dictionary, arraySize * sizeof(char)) ;
if (temp == NULL)
{ fprintf(stderr, "realloc() failed!\n") ;
exit(1) ;
}
dictionary = temp ;
}
dictionary[count] = word ;
count++ ;
qsort( (void *)dictionary, count, sizeof(char), charcompare);
return result;
}
}
int check_word(char *word)
{
int i, len;
/* If length predetermined, check length, if bad, return 0 */
if (strlen(word) > 10)
return SPELL_TOOLONG;
/* Check dictionary first, if found return 3 */
for (i=0; i<count; i++)
{
if ( strcmp(word, dictionary[i]) == 0)
return SPELL_OKAY;
}
len = strlen(word);
/* Now check to see if all characters are alnums.*/
for (i=0; i < len; i++)
{
if (isalnum(word[i]) == 0)
return SPELL_INCORRECT;
}
/* If we get here, the following is true
- word is shorter than 10 in length
- word is not in the dictionary
- word is made up of all alnum characters
Therefore, we return 3
*/
return SPELL_OKAY;
}