我正在尝试在链接列表中的 *add 指针之后添加节点 我的结构和代码如下,但在 (*add)->next = new_node:
中显示错误 typedef struct {
int data;
struct node* next;
}node;
void create_node(node **add,int dat)
{
if((*add) == NULL)
{
(*add) = (node*)malloc(sizeof(node));
(*add)->data = dat;
(*add)->next = NULL;
}
else
{
node *new_node = (node*)malloc(sizeof(node));
new_node->data = dat;
new_node->next = (*add)->next;
(*add)->next = new_node; //assignment to incompatible pointer type error
}
}
最佳答案
您将 next
声明为指向 struct node
的指针,但代码中没有 struct node
(只有 typedef 节点
)。
您需要为该结构指定一个名称,以便在 next
的声明中引用它:
typedef struct node {
目前,struct node
指的是一个不同的、不相关的结构(您尚未定义)。
关于c - 分配给不兼容的指针类型双指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560933/