【问题标题】:Writing to a linked lists with nested structs使用嵌套结构写入链表
【发布时间】:2015-02-07 08:05:35
【问题描述】:

我正在尝试编写一个函数,该函数可以将文件中的一些信息读取到双向链表中的节点中。每个节点数据的格式如下。

结构(命名记录)
艺术家
专辑
歌曲
流派
songLength(这是另一个包含分钟和秒的结构)
播放次数
评分

void load(FILE *file, Node *head)
{
    char tempArtist='\0', tempAlbum='\0', tempTitle='\0', tempGenre='\0'
    ,tempSpace='\0',tempMins='\0',tempSecs='\0';
    SongLength *tempLength=NULL;
    int tempPlay=0, tempRating=0,test=0;
tempLength = (SongLength*)malloc(sizeof(SongLength));

    fscanf(file,"%s",&tempArtist);
    fscanf(file,"%s",&tempAlbum);
    fscanf(file,"%s",&tempTitle);
    fscanf(file,"%s",&tempGenre);
    fscanf(file,"%s",&tempMins);
    fscanf(file,"%s",&tempSecs);
    fscanf(file,"%s",&tempPlay);
    fscanf(file,"%s",&tempRating);
    fscanf(file,"%s",&tempSpace);

    tempLength->mins=tempMins; 
    tempLength->secs=tempSecs;

    head->data->album=tempAlbum; // breaks here
    head->data->artist=tempArtist;
    head->data->genre=tempGenre;
    head->data->song=tempTitle;
    head->data->length=tempLength;
    head->data->played=tempPlay;
    head->data->rating=tempRating;

}

这是我当前的加载函数。当尝试将这些值存储到节点数据中时,我遇到了访问冲突。

这是我的结构以便于复制

typedef struct songlength
{
    int mins;
    int secs;
}SongLength;


typedef struct record
{
    char artist;
    char album;
    char song;
    char genre;
    struct songlength *length;
    int played;
    int rating;

}Record;

typedef struct node
{
    struct node *pPrev;
    struct node *pNext;
    struct record *data;

}Node;

制作节点

Node *makeNode(Record *newData)
{
    Node *temp = NULL;

    temp=(Node*)malloc(sizeof(Node));

    temp->data=newData;
    temp->pNext=NULL;

    return temp;
}

如果出现任何混淆,请告诉我! 这也是我第一次体验动态记忆,所以要温柔:P

谢谢!

【问题讨论】:

  • 当你使用动态内存时,你需要明确地malloc它。当您将 tempLength 声明为指针然后从不为其分配任何内存时,您遇到了问题。
  • @SimonGibbons 初始化头节点时是否尚未分配内存? (主要发生)
  • 是的可能(但我看不到该代码)但是当您执行tempLength->mins=tempMins; 时,您使用的是tempLength,它对您之前为该结构分配的任何内存一无所知。
  • 那么我该如何为我的节点分配内存呢?由于在节点内还有另一个结构,记录。我将在上面包含我的 make 节点函数。

标签: c linked-list


【解决方案1】:

这些行不正确。

fscanf(file,"%s",&tempArtist);
fscanf(file,"%s",&tempAlbum);
fscanf(file,"%s",&tempTitle);
fscanf(file,"%s",&tempGenre);
fscanf(file,"%s",&tempMins);
fscanf(file,"%s",&tempSecs);
fscanf(file,"%s",&tempPlay);
fscanf(file,"%s",&tempRating);
fscanf(file,"%s",&tempSpace);

由于变量的定义方式,它们肯定会导致未定义的行为。

你不能指望

char c = '\0';
fscanf(file, "%s", &c);

工作。 &c 的内存不足,无法读取字符串。你需要这样的东西:

char s[100]; // Or some size that is large enough to hold the data
             // you are about to read.
fscanf(file, "%99s", s); // Make sure that you don't read more than 99
                         // characters. Leave at least one character
                         // for the terminating null character.

我希望这能为您提供有关如何更改变量的足够线索。

【讨论】:

    【解决方案2】:

    您没有为要指向的变量 tempLength 分配内存。

    在访问元素之前添加它

    SongLength *tempLength = malloc(sizeof(struct(SongLength));
    

    编辑

    我只是给出一个如何为您的案例分配和使用嵌套结构的总体思路

    Node *head;
    Record *r=malloc(sizeof(struct record));
    SongLength *s=malloc(sizeof(struct songlength));
    r->length=s;//<----- 1
    r->length->mins=10;//Now you can assign values
    
    head=malloc(sizeof(struct node));
    head->pPrev=NULL;
    head->pNext=NULL;
    head->data=r;//<--- The length member inside record is already assigned memory in 1
    
    head->data->artist='c';
    head->data->length->mins=10;//assign
    

    【讨论】:

    • 这项工作,但在尝试将扫描的数据分配给节点时,它继续中断。我还需要 malloc 我制作的每个临时变量吗?
    • @user348104 您必须在record 中为songlength 分配内存,然后才能将其传递给makeNode
    • char 只能保存one character。您必须使用数组作为 sahu 的回答状态
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多