gpt4 book ai didi

c - 在链表中插入节点 C : expected node_t * but argument is of type node_t

转载 作者:太空宇宙 更新时间:2023-11-04 03:09:19 25 4
gpt4 key购买 nike

我无法将新列表插入到我的结构中。有人可以帮帮我吗?谢谢! :)这是我的代码和错误:

编译后报错:

27:9:错误:从类型“node_t * {aka struct _node_t *}”分配给类型“node_t {aka struct _node_t}”时类型不兼容

代码:

typedef struct _node_t {
double d;
struct _node_t *next;
} node_t;

void print_list (node_t *l) {
node_t *curr = l;
printf("[");

while (curr != NULL) {
if (l != curr) printf (",");
printf("%4.1f",curr->d);
curr = curr->next;
}

printf("]\n");
}

node_t *insert (node_t *l, double d) {
node_t *new_node;
new_node = (node_t *) malloc (sizeof(node_t));

if (new_node == NULL) {
printf("insert: error: no space left\n");
return l;
}

new_node->d = d;
new_node->next = l;
return new_node;
}

int main (void)
{
node_t n1;

print_list(&n1);
n1=insert(n1,10);
}

最佳答案

函数 insert 的返回类型 node_t * 是一个指针类型。

但是在 main 中,您试图将返回的指针分配给非指针类型的对象。

node_t n1;
//...
n1=insert(n1,10);

此外,对象 n1 未初始化。

你需要在main里写

node_t *n1 = NULL;
print_list( n1 );
n1=insert( n1, 10 );

而且似乎最好在 main like 中交换最后两个语句

node_t *n1 = NULL;
n1=insert( n1, 10 );
print_list( n1 );

关于c - 在链表中插入节点 C : expected node_t * but argument is of type node_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58221375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com