0
我想写一个函数,可以添加新的结构到链表的末尾。而它总是出现分段错误。C:链接列表推段错误
void
push(stk_t *stklist, info_t *gds)
{
stk_t *current = stklist;
if (current == NULL)
{
current->gds = (info_t *)malloc(sizeof(info_t));
current->gds = gds;
current->next = (stk_t *)malloc(sizeof(stk_t));
current->next = NULL;
}
else
{
while (current != NULL)
{
current = current->next;
}
current->next = (stk_t *)malloc(sizeof(stk_t));
current->next->gds = (info_t *)malloc(sizeof(info_t));
current->next->gds = gds;
current->next->next = (stk_t *)malloc(sizeof(stk_t));
current->next->next = NULL;
}
}
我的结构
typedef struct{
char name[NAME_SIZE];
char aisle;
int shelf;
int weight;
int price;
int quantity;
} info_t;
typedef struct stk stk_t;
struct stk{
info_t *gds;
stk_t *next;
};
功能推的目的()是第二个参数添加到链接列表的末尾。
提示:当你做'电流 - > GDS =(info_t *)malloc(sizeof(info_t));','current'具有什么值? – molbdnilo
'if(current == NULL) { current-> gds' - 解引用NULL指针。 – szczurcio
@molbdnilo糟糕,我打破了你的暗示。当我发布时没有看到它,对不起。 – szczurcio