void search(char*** p, int numOfWords, int* pNumOfDefArr){
int i, j, index;
char* word = (char*)malloc(WORD_SIZE * sizeof(char));
for (i = 0; i < N; i++) //just clearing the screen
printf("\n");
printf("Hello and thank you for filling Dictionary 1.0 with new words!!\n");
printf("Which word are you looking for??\n");
gets(word);
fix_word(word, 0);
while (strcmp(word, "Exit")){
index = (search_word(p, word, 0, numOfWords - 1, 0));
if (index < 0)
printf("Unknown word!!!!!!\n");
else{
for (j = 0; j < pNumOfDefArr[index]; j++)
printf("%s\n", *(*(p + index) + 1 + j));
}
free(word);
char* word = (char*)malloc(WORD_SIZE * sizeof(char));
printf("Looking for another word?\n");
gets(word);
fix_word(word, 0);
}
printf("Farewell!!\n");
在调试器上我可以看到第10行:while (strcmp(word, "Exit"))
word的值是
从&#34; asd&#34; 更改为&#34;读取字符串字符时出错。&#34; 为什么?
以下是fix_word()
功能的代码:
void fix_word(char* pword, int j){
if (*(pword + j) != '\0'){
if (j == 0 && (*(pword + j) >= 'a' && *(pword + j) <= 'z')){
*pword -= N;
j++;
}
else if (*(pword + j) >= 'A' && *(pword + j) <= 'Z'){
*(pword + j) += N;
j++;
}
else
j++;
fix_word(pword, j);
}
}
答案 0 :(得分:6)
这称为范围。
char* word = (char*)malloc(WORD_SIZE * sizeof(char)); /* 1 */
while (strcmp(word, "Exit")){ /* 1 */
free(word); /* 1 and gone */
char* word = (char*)malloc(WORD_SIZE * sizeof(char)); /* 2 */
}
你已宣布变量&#34; word&#34;在两个不同的范围内,你使用它混合。
如果省略第二个&#34; char *&#34;,一切都会好的。