我是该站点的新手,我正在寻找有关存储 Mp3 与艺术家姓名、歌曲名称、专辑名称、日期和运行时间的双向链接列表的帮助。如果您能帮助我,我将不胜感激。根据 GDB,我在 add 函数中遇到了段错误。
警告也如下:
Description Resource Path Location Type
'data' is used uninitialized in this function [-Wuninitialized] Mp3.c /OperatingSystemsLab1
line 7 C/C++ Problem
assignment makes pointer from integer without a cast [enabled by default] Mp3.c /OperatingSystemsLab1
line 7 C/C++ Problem
implicit declaration of function 'malloc' [-Wimplicit-function-declaration] Mp3.c /OperatingSystemsLab1
line 21 C/C++ Problem
assignment makes pointer from integer without a cast [enabled by default] Mp3.c /OperatingSystemsLab1
line 8 C/C++ Problem
implicit declaration of function 'free' [-Wimplicit-function-declaration] Mp3.c /OperatingSystemsLab1
line 40 C/C++ Problem
incompatible implicit declaration of built-in function 'malloc' [enabled by default] Mp3.c /OperatingSystemsLab1
line 21 C/C++ Problem
incompatible implicit declaration of built-in function 'malloc' [enabled by default] Mp3.c /OperatingSystemsLab1
line 57 C/C++ Problem
incompatible implicit declaration of built-in function 'free' [enabled by default] Mp3.c /OperatingSystemsLab1
line 40 C/C++ Problem
我的代码如下
#include <stdio.h>
#include "Mp3.h"
void add(struct MP3 *pointer, char artistname, char albumname, char songname, int date, int runtime){
/*make structure for new data*/
struct MP3 *data;
data->artistName = artistname;
data->albumName = albumname;
data->date=date;
data->runTime=runtime;
data->next=NULL;
data->prev=NULL;
if(pointer->next==NULL) {
pointer->next = data;
}
else {
while(pointer->next != NULL){
pointer = pointer->next;
}
pointer->next = (struct MP3*)malloc(sizeof(struct MP3));
pointer->next = data;
struct MP3 *temp = pointer;
pointer = pointer->next;
pointer->prev = temp;
}
};
void delete(struct MP3 *pointer, char *artistname){
while(pointer->next!=NULL && (pointer->next)->artistName!=artistname){
pointer = pointer->next;
}
if(pointer->next==NULL){
return;
}
struct MP3 *temp;
temp = pointer->next;
pointer->next = temp->next;
pointer->next->prev = pointer;
free(temp);
};
void print(struct MP3 *pointer){
if(pointer==NULL){
return;
}
printf("Artist name: %s \n", pointer->artistName);
printf("Album name: %s \n", pointer->albumName);
printf("Title: %s \n", pointer->songName);
printf("Date: %d \n", pointer->date);
printf("Runtime: %d\n", pointer->runTime);
print(pointer->next);
};
int main(){
struct MP3 *start,*current;
start = (struct MP3 *)malloc(sizeof(struct MP3));
current = start;
current->next = NULL;
current->prev = NULL;
printf("1. Add\n");
printf("2. Delete\n");
printf("3. Print\n");
while(1)
{
int query;
scanf("%d",&query);
if(query==1)
{
int rt,d;
char artn,albn,sn;
scanf("%c %c %c %d %d",&artn,&albn,&sn,&d,&rt);
add(start,artn,albn,sn,d,rt);
}
else if(query==2)
{
char artn;
scanf("%c",&artn);
delete(start,&artn);
}
else if(query==3)
{
printf("The list is ");
print(start->next);
printf("\n");
}
}
};
和头文件
#ifndef MP3_H_
#define MP3_H_
struct MP3{
char *artistName;
char *albumName;
char *songName;
int date;
int runTime;
struct MP3 *next;
struct MP3 *prev;
};
#endif /* MP3_H_ */
就像我说的,我是新人,所以请怜悯我。任何帮助将不胜感激!谢谢!