我正在学习如何在C中实现链表。我了解常规链表的基本知识,如何添加值,如何打印它们等,但我一直在想 - 是吗?可以添加其他结构作为链接列表中的值?我的意思是:C中的链表中的结构
typedef struct personal_info {
char *name;
char *surname;
int phone_number;
} Info;
typedef struct llist {
Info *info;
struct llist *next;
} List;
而且当我这样做,我怎么访问Info
结构的值是多少?
List *l;
l = malloc(sizeof(List));
l->info->name = 'name';
l->info->surname = 'surname';
l->info->phone_number = 1234567890;
该代码崩溃,所以我肯定做错了什么。你能给我一些提示如何实现这一点?
你为节点分配了内存吗? (信息字段) –
您需要将内存分配给您的信息*。 –