当头部被删除并向前移动时,链表不会 return 到 main 函数
Linked List won't return to main function when head is deleted and moved forward
我目前在编写字典项目时停留在这部分。我几乎可以轻而易举地完成其他所有事情,但在这里。
我的问题是,每当我 return 主函数的字典(一个链表头)在它通过这个特定的 if 语句后,就会弹出一个异常。
item *deleteEntry(item *entry, item *dictionary)
{
item *cont,*temp;
int check;
check = 0;
for (cont = dictionary; cont != NULL; cont = cont->next)
{
if (!strcmp(cont->word, entry->word) && cont == dictionary)
{
dictionary = cont->next;
free(cont);
check = 1;
break;
}
//other non-problematic entry-deletion statements
}
if (!check)
return 0;
return dictionary;
}
即使它在函数中正确地存储了数据(我已经通过打印测试过),但当return编辑到主要功能。
这是我调用函数的部分
printf("\nEnter word to delete entry: ");
getchar();
fgets(entry->word, WORDLIMIT, stdin);
if (dictionary[toupper(entry->word[0]) - 65] == NULL)
printf("\nWord Does not exist");
else
{
cont = deleteEntry(entry, dictionary[toupper(entry->word[0]) - 65]);
if (cont)
dictionary[toupper(entry->word[0] - 65)] = cont;
else
printf("\nError: Word doesn't exist in archive %d!!\n", toupper(entry->word[0]));
例外情况是:
Access violation reading location 0xDDDDDDDD. (I don't know what this is, even after google searches. please enlighten me)
这是我第一次 post 来这里,所以我会借此机会说声谢谢,这是我遇到困难时第一个去的地方:)
问题已解决!
/* this is part of your code. check comments added by me
*/
for (cont = dictionary; cont != NULL; cont = cont->next /*->next can be null*/)
{
if (!strcmp(cont->word, entry->word) && cont == dictionary)
{
dictionary = cont->next; /*if ->next above is null than boom (null->null)*/
free(cont);
check = 1;
break;
}
//other non-problematic entry-deletion statements
}
/* better use this */
cont = dictionary->next;
while(cont){
if(...){
dictionary=cont->next;
free(cont);
break; /* or return something*/
}
cont=cont->Next;
}
/*return other thing*/
正如我评论的那样,我认为重点是 dictionary[toupper(entry->word[0] - 65)]
此代码导致越界访问您的指针数组。
例如
entry->word[0] = 'a'
toupper(entry->word[0] - 65) = 32
但我认为你的数组有 26 个元素。
一定是
dictionary[toupper(entry->word[0]) - 65]
我目前在编写字典项目时停留在这部分。我几乎可以轻而易举地完成其他所有事情,但在这里。
我的问题是,每当我 return 主函数的字典(一个链表头)在它通过这个特定的 if 语句后,就会弹出一个异常。
item *deleteEntry(item *entry, item *dictionary)
{
item *cont,*temp;
int check;
check = 0;
for (cont = dictionary; cont != NULL; cont = cont->next)
{
if (!strcmp(cont->word, entry->word) && cont == dictionary)
{
dictionary = cont->next;
free(cont);
check = 1;
break;
}
//other non-problematic entry-deletion statements
}
if (!check)
return 0;
return dictionary;
}
即使它在函数中正确地存储了数据(我已经通过打印测试过),但当return编辑到主要功能。
这是我调用函数的部分
printf("\nEnter word to delete entry: ");
getchar();
fgets(entry->word, WORDLIMIT, stdin);
if (dictionary[toupper(entry->word[0]) - 65] == NULL)
printf("\nWord Does not exist");
else
{
cont = deleteEntry(entry, dictionary[toupper(entry->word[0]) - 65]);
if (cont)
dictionary[toupper(entry->word[0] - 65)] = cont;
else
printf("\nError: Word doesn't exist in archive %d!!\n", toupper(entry->word[0]));
例外情况是:
Access violation reading location 0xDDDDDDDD. (I don't know what this is, even after google searches. please enlighten me)
这是我第一次 post 来这里,所以我会借此机会说声谢谢,这是我遇到困难时第一个去的地方:)
问题已解决!
/* this is part of your code. check comments added by me
*/
for (cont = dictionary; cont != NULL; cont = cont->next /*->next can be null*/)
{
if (!strcmp(cont->word, entry->word) && cont == dictionary)
{
dictionary = cont->next; /*if ->next above is null than boom (null->null)*/
free(cont);
check = 1;
break;
}
//other non-problematic entry-deletion statements
}
/* better use this */
cont = dictionary->next;
while(cont){
if(...){
dictionary=cont->next;
free(cont);
break; /* or return something*/
}
cont=cont->Next;
}
/*return other thing*/
正如我评论的那样,我认为重点是 dictionary[toupper(entry->word[0] - 65)]
此代码导致越界访问您的指针数组。
例如
entry->word[0] = 'a'
toupper(entry->word[0] - 65) = 32
但我认为你的数组有 26 个元素。
一定是
dictionary[toupper(entry->word[0]) - 65]