c - 链表添加节点失败

标签 c list pointers

我想创建一个链表,初始化它并在开头添加一个新节点。 但是我有这个问题:

//// 错误:请求成员“值”不是结构或 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/

相关文章:

c - 使用scanf函数

c - c 中的这个堆排序程序不起作用?我使用指针来编辑数组元素

python - Tkinter 选项菜单

C++ 引用/指针

c - 使用 strcpy 时如何将空终止符添加到 char 指针

c# - 使用后期绑定(bind)获取 List<T> 值

python - 从两个列表中创建一个新列表,其中一个列表缺少数据 - 只应接受有效的对应值

c++ - const char * 与其他指针不同吗?

代码在循环的第二次运行时停止

c - 循环中的第二个 Scanf 未被读取。跳转到 else 条件