0

我是 C 中链表的新手,我遇到的问题是我正在尝试创建一个字符串的链表,但是当我尝试打印该列表时,它会从两个不同的字符串中打印第一个字符。我想我弄乱了一些指针。请问有什么帮助吗?这是我的代码...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
    typedef struct _song{char *songTitle; char *songAuthor; char *songNote; struct _song *next;}SONG;

    int songCount =4;


    char SongTitle[songCount];
    char AuthorName[songCount];
    char SongNotes[songCount];


    char songTitle0[21] = "19 problems";
    char songArtist0[21]="JayZ";
    char songNotes0[81]="JiggaWhoJiggaWhat";
    SongTitle[0]=*songTitle0;//points at string songTitle0
    AuthorName[0]=*songArtist0;
    SongNotes[0]=*songNotes0;

    char songTitle1[21] = "Cig Poppa";
    char songArtist1[21]="Biggie Smalls";
    char songNotes1[81]="I Luv it When you call me big poppa";
    SongTitle[1]=*songTitle1;
    AuthorName[1]=*songArtist1;
    SongNotes[1]=*songNotes1;

    SONG *CurrentSong, *header, *tail;

    int tempCount=0;
    header = NULL;

    for(tempCount=0;tempCount<songCount;tempCount++)
    {

        CurrentSong = malloc(sizeof(struct _song));
        CurrentSong->songTitle= &SongTitle[tempCount];
        CurrentSong->songAuthor=&AuthorName[tempCount];
        CurrentSong->songNote=&SongNotes[tempCount];

        if(header == NULL)
        {
            header=CurrentSong;//head points to first thing in memory
        }
        else
        {
            tail->next=CurrentSong;
        }
        tail = CurrentSong;//always the last thing in the list 
        tail->next=NULL;//the next pointer is null always

    }
    tempCount =0;
    for(CurrentSong=header; CurrentSong!=NULL; CurrentSong=CurrentSong->next)
                    {
                        printf("\n%d: ", tempCount);
                            printf("Title: %s ",CurrentSong->songTitle);


                        printf("Author: %s ",CurrentSong->songAuthor);
                        tempCount++;
                    }


    return 0;
}
4

3 回答 3

0
SongTitle[0]=*songTitle0;//points at string songTitle0

评论不属实。您正在将第一个字符复制songTitle0SongTitle.

你的设置太复杂了。您只需将songTitle0不带任何*or分配给列表的第一个链接&songTitle元素;两者都是 type char*,所以这只是一个指针副本。跳过SongTitle和变量,它们没有任何作用AuthorNameSongNotes

于 2011-11-20T22:57:31.747 回答
0

SongTitle、AuthorName 和 SongNotes 这三个变量是 的数组char,而不是 的数组string。您需要将他们的声明更改为:

char* SongTitle[songCount];
char* AuthorName[songCount];
char* SongNotes[songCount];

然后,您需要像这样更新它们:

SongTitle[0] = songTitle0;//points at string songTitle0
AuthorName[0] = songArtist0;
SongNotes[0] = songNotes0;

当您将它们存储在链表中时:

    CurrentSong = malloc(sizeof(struct _song));
    CurrentSong->songTitle = SongTitle[tempCount];
    CurrentSong->songAuthor = AuthorName[tempCount];
    CurrentSong->songNote = SongNotes[tempCount];
于 2011-11-20T22:59:39.577 回答
0

这不是您应该使用链接列表的方式。

它是

typedef struct list {
    void *data;
    struct list *next;
}
SONG *s = (SONG *)songList->data;

同样,对于克隆字符串,您需要使用strdup.

例如

s->songTitle = strdup(SongTitle);
s->songAuthor = strdup(AuthorName);
s->songNote = strdup(SongNotes);

完成后不要忘记free琴弦。

于 2011-11-20T23:00:01.343 回答