0
2d 연결된 목록을 만들려고합니다. 해당 요소에 액세스하려고하면 세그먼트 화 오류가 발생합니다. 여기2d 연결된 목록 요소를 만들고 액세스하는 방법
나는 디버깅 만든 코드
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;
}
}
의 일부이다.
이 줄은 if(strcmp(pairPtr->data, word2) == 0)
가 왜 if(strcmp(ptr->data, word1) == 0)
가 작동하지만 위되어 나에게 분할 오류를 준다? 이 문제를 해결하려면 어떻게해야합니까?
문제 pairPtr-> data''하지만 그것이'NULL' 수해서는 안'while' 문에서'NULL' 검사가있다. – Pareidolia
'while' 문에서'pairPtr'이'NULL'이 아니라는 것을 확인합니다. 당신은'data' 필드가'NULL'이 아닌지 확인하지 않습니다. – LiranT