Scanf into struct包含指向不同结构的指针 [英] Scanf into struct containing pointer to different struct
本文介绍了Scanf into struct包含指向不同结构的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个函数将书籍信息插入到结构中..它运作良好但是当扫描日期程序崩溃时...我搜索了很多但无法修复它
typedef struct
{
char title [10];
char author [10];
char publisher [10];
char ISBN [10];
dateStruct * date;
int份;
int current;
}预订;
typedef struct
{
int day;
int month;
int year;
} dateStruct;
book * insert(void)
{
book * theInserted =(book *)malloc(1 * sizeof(book));
得到(theInserted->标题);
得到(theInserted->作者);
得到(theInserted->发布商);
得到(theInserted-> ISBN);
scanf(%d%d,&(theInserted-> copies),&(theInserted-> current));
scanf(%d%d%d,&(theInserted-> date-> day),&(theInserted-> date-> month),&(theInserted->日期 - >年));
返回插入;
}
我的尝试:
这是出错的地方:
scanf(%d%d%d,&(theInserted - > date-> day),&(theInserted-> date-> month),&(theInserted-> date-> year));
解决方案
您必须为theInserted-> date
分配内存,并在不再使用时释放它:
book * theInserted =(book *)malloc(sizeof(book));
theInserted-> date =(dateStruct *)malloc(sizeof(dateStruct));
/ *使用此处插入* /
/ *可用内存* /
免费(theInserted-> date);
free(theInserted);如果不这样做,
theInserted-> date
成员将指向导致访问冲突的任何地方或只是一个程序崩溃。
更好的解决方案是使dateStruct
成员成为普通类型的成员,而不是指针:
typedef struct
{
char title [10];
char author [10];
char publisher [10];
char ISBN [10];
dateStruct日期; //普通成员而不是指针
int copies;
int current;
}预订;
/ * ... * /
scanf(%d%d%d,&(theInserted-> date.day),&(theInserted - > date.month),及(theInserted-> date.year));
this function inserts a book info into structure .. it works well but when scanning the date the program crushes..I searched a lot but couldn't fix it
typedef struct { char title[10]; char author[10]; char publisher[10]; char ISBN[10]; dateStruct* date; int copies; int current; } book; typedef struct { int day; int month; int year; } dateStruct; book* insert(void) { book* theInserted =(book*)malloc(1*sizeof(book)); gets(theInserted->title); gets(theInserted->author); gets(theInserted->publisher); gets(theInserted->ISBN); scanf("%d%d",&(theInserted->copies),&(theInserted->current)); scanf("%d%d%d",&(theInserted->date->day),&(theInserted->date->month),&(theInserted->date->year)); return theInserted; }
What I have tried:
this is where something goes wrong:
scanf("%d%d%d",&(theInserted->date->day),&(theInserted->date->month),&(theInserted->date->year));
解决方案
You must allocate memory for thetheInserted->date
and free it when no longer used:
book* theInserted =(book*)malloc(sizeof(book)); theInserted->date = (dateStruct*)malloc(sizeof(dateStruct)); /* Use theInserted here */ /* Free memory */ free(theInserted->date); free(theInserted);When not doing so, the
theInserted->date
member points to anywhere resulting in an access violation or just a program crash.
A better solution is to make thedateStruct
member a normal type member and not a pointer:
typedef struct { char title[10]; char author[10]; char publisher[10]; char ISBN[10]; dateStruct date; // normal member and not pointer int copies; int current; } book; /* ... */ scanf("%d%d%d",&(theInserted->date.day),&(theInserted->date.month),&(theInserted->date.year));
这篇关于Scanf into struct包含指向不同结构的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文