我想创建一个链表,初始化它并在开头添加一个新节点。 但是我有这个问题:
//// 错误:请求成员“值”不是结构或 union ////
t_bool list_add_elem_at_front(t_list *front_ptr, double elem)
{
if (front_ptr == NULL)
{
front_ptr = malloc(sizeof(*front_ptr));
front_ptr->value = elem;
front_ptr->next = NULL;
}
printf("%f\n", front_ptr->value);
return (TRUE);
}
我确定该结构是 malloc 但我真的不明白为什么它在结构上找不到“值”和“*下一个”
int main(void)
{
int i = 2.1;
t_list list_head = NULL;
list_add_elem_at_front(&list_head, i);
}
和头文件
typedef struct s_node
{
double value;
struct s_node *next;
} t_node;
typedef t_node *t_list;
最佳答案
试试这个
t_bool list_add_elem_at_front(t_list *front_ptr, double elem)
{
if (*front_ptr == NULL)
{
*front_ptr = malloc(sizeof(**front_ptr));
(*front_ptr)->value = elem;
(*front_ptr)->next = NULL;
}
printf("%f\n", (*front_ptr)->value);
return (TRUE);
}
关于c - 链表添加节点失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21027933/