【发布时间】:2014-03-14 17:12:38
【问题描述】:
我在这段带有链表的代码中遇到了问题。它给了我错误:
Segmentation Fault (Core Dumped)
谁能看出问题出在哪里?提前致谢。
pos 代表位置,root 代表第一个元素
typedef struct
{
element *root;
int size;
} list;
typedef struct _element
{
char *str;
struct _element *next;
} element;
int
insert_list (list * lst, const char *value, int pos)
{
element *new;
int k;
new = malloc (sizeof (element));
for (k = 0; k < pos; k++)
{
new = lst->root;
lst->root = lst->root->next;
if (k == pos - 1)
{
lst->root = NULL;
new->str = value;
}
}
for (k = 0; k <= lst->size; k++)
{
new = lst->root;
lst->root = lst->root->next;
if (k == lst->size)
{
lst->root = NULL;
new->str = value;
}
if (pos < 0 || pos >= lst->size)
return -1;
else
return pos;
}
}
【问题讨论】:
-
什么是
first?您在任何分配之前取消引用它! -
显示你如何调用函数。
-
你好,你是对的,我忘了声明变量,但问题仍然存在,你知道吗?
-
函数调用正确,既然已经给我们了,我们只需要开发函数
-
lst == NULL ?和 new = malloc(sizeof(element)); for(k=0;k
root;很奇怪
标签: c linked-list segmentation-fault