Ich versuche, 2d verknüpfte Liste zu erstellen. Wenn ich versuche, auf sein Element zuzugreifen, wird ein Segmentierungsfehler verursacht.So erstellen und Zugriff auf 2d verknüpfte Listenelemente
Hier ist ein Teil des Codes
struct Node{
char *data;
int count;
struct Pair *p;
struct Node *next;
};
struct Pair{
char *data;
int count;
struct Pair *next;
};
void insertPairs(char *word1, char *word2, struct Node **head){
struct Node *ptr = *head;
while(ptr != NULL) {
if(strcmp(ptr->data, word1) == 0){
struct Pair *pairPtr = ptr->p;
while(pairPtr != NULL){
if(strcmp(pairPtr->data, word2) == 0){ //Segmentation Fault
pairPtr->count = pairPtr->count + 1;
break;
}
}
struct Pair *tmp = (struct Pair*) malloc(sizeof(struct Pair));
tmp->data = word2;
tmp->count = 1;
tmp->next = pairPtr;
pairPtr = tmp;
break;
}
ptr = ptr->next;
}
}
ich einige Debug gemacht.
Diese Linie gibt mir Segmentation Fault if(strcmp(pairPtr->data, word2) == 0)
Warum ist die if(strcmp(ptr->data, word1) == 0)
arbeiten, aber oben nicht? Was soll ich tun, um das zu beheben?
Problem ist 'pairPtr-> data' aber es gibt ein' NULL' Check in 'while' Anweisung, damit es nicht' NULL' sein sollte. – Pareidolia
In der 'while'-Anweisung verifizieren Sie nur, dass' pairPtr' nicht 'NULL' ist. Sie verifizieren nicht, dass das 'Daten'-Feld nicht' NULL' ist. – LiranT