我有 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_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;
}
你从来没有alloc
过在你的Link
结构中name
或path
。 您正在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);