現在の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;
}
それが重要ならば、ここで参照2つの構造体です:私は、セグメンテーションフォルトを引き起こすことが
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;
何をしているのですか?
デバッガーを試しましたか? –
これを2つの部分に分割する必要があります.1つは再生リストを検索する部分と、もう1つは再生リストを追加する部分です。そうすれば、どちらが間違っているのかをより簡単に知ることができます。すべてのフィールドの代わりに 'struct song'を引数として渡すこともできます。 – Ryan
投稿された(メインエントリポイントなし)ので、コードはセグメンテーションフォールトを行いません.... – jpo38