我有2个结构:
第一个:
struct Link
{
frame_t *frame;
struct Link *next;
};
typedef struct Link link_t;
第二个:
struct Frame
{
char *name;
unsigned int duration;
char *path; // may change to FILE*
};
typedef struct Frame frame_t;
我尝试使用函数为Link结构分配内存,但它不起作用。
致电功能:
link_t* list = NULL;
list = AddItemToList(list);
这就是功能:
link_t* AddItemToList(link_t* item)
{
if (item == NULL)
{
item = (link_t*)calloc(1, sizeof(link_t) );
item->frame = (frame_t*)calloc(1, sizeof(frame_t));
printf("Please insert frame path: ");
scanf("%s", item->frame->path);
printf("Please insert frame duration(in miliseconds): ");
scanf("%d", item->frame->duration);
printf("Please choose a name for that frame: ");
scanf("%s", item->frame->name);
}
else item->next = AddItemToList(item-> next);
return item;
}
答案 0 :(得分:3)
您的alloc
结构中永远不会name
'path
或Link
。你scanf
进入了未初始化的记忆。
item->frame = (frame_t*)calloc(1, sizeof(frame_t));
// You need to add something like:
item->frame->name = (char *)calloc(1, some_string_size);
item->frame->path = (char *)calloc(1, some_string_size);