【发布时间】:2016-11-04 05:44:46
【问题描述】:
我正在为我当前的 CS 类编写一个涉及链表的程序,特别是一个函数在我调用它时会导致分段错误。函数如下:
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;
}
如果重要的话,这里是引用的两个结构:
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作为参数传递,而不是单独传递其所有字段。 -
发布后(没有主入口点),代码不会出现任何分段错误......
-
无论如何,一个问题是你没有初始化你添加的歌曲的
nexts。正如 MD XF 指出的那样,调试器在这里可以提供很大帮助;即使你不知道如何使用它来检查程序的状态,它至少会告诉你段错误发生在哪一行。
标签: c struct linked-list segmentation-fault