【发布时间】:2019-06-20 00:27:59
【问题描述】:
我正在尝试在以下代码中解决“分段错误”的问题, 我可能认为我没有得到全貌,这就是为什么我不断获得分段错误而不是分段错误, 任何有助于深入理解这一点的帮助都会鼓励我进行自我分析。
代码应该简单明了:给定两个列表,我想从第一个列表中删除第二个列表中出现的所有元素, 我的努力是:
typedef struct EL {
int info;
struct EL *next;
} ElementoLista;
typedef ElementoLista *ListaDiElementi;
void filterLists(ListaDiElementi *lista1,ListaDiElementi *lista2) {
ListaDiElementi aux = *lista1,aus = *lista2,corr;
while(aux != NULL) {
if(aux->info == aus->info) { // Erase from the first
corr = aux;
aux = aux->next;
free(corr);
}
else {
if(aus != NULL) //Increase the second
aus = aus->next;
else {
aus = *lista2; //Restart
aux = aux->next;
}
}
}
}
【问题讨论】:
-
不要不在 typedef 中隐藏指针。它会导致问题:
typedef ElementoLista* ListaDiElementi; -
@PaulOgilvie 不要认为那是问题,但是好的,谢谢
-
如果 aux 不为 null 但 aus 为 null aux->info==aus->info 会导致问题吗?
-
应该进入else的else,不是吗?两个列表总是至少有一个元素@Spinkoo
-
这似乎是learn how to debug your programs 的好时机。
标签: c pointers linked-list free