First of all, in C you cannot compare strings with comparison operators. They only compare pointers, not the string data. You must use strcmp , as in strcmp(*mid, sample[0]) != 0 .
That is very useful, I have only just learned about strcmp and keep forgetting that I can use it.Next, I am afraid that dict[i] are not what you think they are. They are dict array values, that is character pointers, which point to a more or less random locations. Each is valid, points a particular word, but the arithmetics on them makes no sense, and will cause crashes. What you want is dict array locations, that is &dict[i]. Now the warnings will go away.
Beware that your code may try to access dict[20], which is beyond the array.
I think I tried something like what you have suggested, but came unstuck with the arithmetic - trying to find the midpoint value between the addresses sometimes led to an address that was not the beginning of a string, if you understand what I mean.
I have actually come across a function called bsearch which is in stdlib.h that is a more efficient way of doing the search, and have created a new program that does this:
/* binary search for spellchecker */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
int i;
char * string;
char dict[20][20] = {"aardvark","bloomers","chopsticks","dinner","entry","figure","great","hand","igloo","journey","kennel","lemon","money","noon","open","pantry","queen","tomatoes","vinegar","xylophone"};
char sample[3][20] = {"vinegar","bloomers","jaunty"};
for (i=0; i<3; i++)
{
string = (char*) bsearch (sample[i], dict, 20, 20, (int(*)(const void*,const void*)) strcmp);
if (string!=NULL)
{printf ("%s is in the dictionary\n",string);}
else
{printf ("%s is not in the dictionary\n",sample[i]);}
}
return 0;
}
However, I have not yet figured out a way to incorporate this code into my earlier program. I will be looking into this over the next few days. Finally, I would highly recommend to restructure your program a little bit.
A. read_words and read_text are in fact the same function; they just fill up different arrays. Get rid of one of them.
B. Do not pass the whole sample array to comparison routine. It only needs one word at a time.
C. comparison is a misnomer. It should be called search .
Thank you for all these final suggestions, especially A. I did not realise that I could use the same function for the two different files.
You have given me a lot of things to think about - many thanks!