Subj : Re: strange string pointer to strings...? To : borland.public.cpp.borlandcpp From : maeder Date : Fri Sep 30 2005 07:17 pm "mujeebrm" writes: > this is original code example which i had modified and posted from > the book C++ The Complete Reference,3rd ed. written by Herb Schildt. I suspected that. This author is notorious for the lack of quality of his books. > int main(void) > { > char word[80], ch; > char **p; > > do { > puts("\nEnter word: "); > scanf("%s", word); This is just as bad as using gets(). If the character sequence in the input buffer is too long, the of word will be overrun. The *scanf() functions allow the maximum number of input characters to be specified for each conversion; e.g. "%79s". Also, scanf() returns a value indicating success of the input operation. It is wrong not to check that value. > p = (char **)dic; This cast is a recipe for disaster. Because the program is lying to the compiler. If the type of dic is changed to char const *dic[] = ... and that of p to char const **p; then the assignment p = dic; compiles without the cast. And the program has a chance to work correctly. > > /* find matching word and print its meaning */ > do { > if(!strcmp(*p, word)) { > puts("Meaning:"); > puts(*(p+1)); > break; > } > if(!strcmp(*p, word)) break; This line is completely superfluous. > p = p + 2; /* advance through the list */ > } while(*p); > if(!*p) puts("Word not in dictionary."); > printf("Another? (y/n): "); > scanf(" %c%*c", &ch); Another ignored return value ... .