현재 CS 클래스에 대한 링크 된 목록이 포함 된 프로그램을 작성 중이며 특히 한 함수는 호출 할 때 세그먼트 화 오류를 계속 발생시킵니다. 이 두 개의 구조체가 참조 여기에, 중요한 경우Segfault가 C에 연결된 목록이 있습니다.
void addSong(Playlist *theList, char *name, char *title, char *artist, int minutes, int seconds) {
/*
1. Make sure a playlist by that name exists (so you can add a song to it)
2. Make sure the song does not already exist in the playlist (title/artist)
3. Add the new song to the end of the songlist in that playlist (add-at-end)
*/
Playlist *Pointer = theList;
while(1){//Find the list
if(strcmp(Pointer->name, name) == 0)
break;
if(Pointer->next == NULL){
printf("There is no playlist by that name.\n");
return;
}
Pointer = Pointer->next;
}
Song *playPoint = Pointer->songlist;
while(1){//Find the end of the list
if(playPoint == NULL){
Song *Songy = malloc(sizeof(Song));
Songy->title = title;
Songy->artist = artist;
Songy->minutes = minutes;
Songy->seconds = seconds;
Pointer->songlist = Songy;
}
if(strcmp(playPoint->title, title) == 0 && strcmp(playPoint->artist, artist) == 0){
printf("There is already a song by that title and artist.");
return;
}
if(playPoint->next == NULL){
break;
}
playPoint = playPoint->next;
}
Song *Songy = malloc(sizeof(Song));
Songy->title = title;
Songy->artist = artist;
Songy->minutes = minutes;
Songy->seconds = seconds;
playPoint->next = Songy; //Add the song to the end of the list
return;
}
:이 기능은 아래에 내가 segfault의 원인이 뭐하는 거지
typedef struct song {
char *title;
char *artist;
int minutes;
int seconds;
struct song *next;
} Song;
typedef struct playlist {
char *name;
Song *songlist;
struct playlist *next;
} Playlist;
?
디버거를 사용해 보았습니까? –
재생 목록을 찾는 부분과 노래를 추가하는 부분으로 나눠야합니다. 그렇게하면 어느 것이 잘못 됐는지를 쉽게 알 수있을뿐만 아니라 일반적으로 더 깨끗합니다. 또한'struct song'을 모든 필드 대신에 인수로 전달할 수 있습니다. – Ryan
게시 됨 (주 진입 점 없음)으로 코드가 세그먼테이션 오류가 발생하지 않습니다 .... – jpo38