C 简单链表代码中的错误
我尝试用整数向量创建一个简单的链表。为什么这不起作用?我得到的错误是:C 简单链表代码中的错误,c,pointers,struct,C,Pointers,Struct,我尝试用整数向量创建一个简单的链表。为什么这不起作用?我得到的错误是: '=' : incompatible types - from 'talstrul *' to 'node *' '=' : cannot convert from 'talstrul*' to talstrul' 这是.h文件: #include <stdio.h> #include <stdlib.h> typedef struct { int num; struct node
'=' : incompatible types - from 'talstrul *' to 'node *'
'=' : cannot convert from 'talstrul*' to talstrul'
这是.h文件:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int num;
struct node *next;
} talstrul;
#包括
#包括
类型定义结构
{
int-num;
结构节点*下一步;
}距骨;
这是.c文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "clabb2head.h"
int main()
{ int vek[5] = {1,2,3,4,5};
int langd = sizeof(vek)/sizeof(vek[0]);
printf("%d",langd);
talstrul *temp = malloc(sizeof(talstrul));
talstrul *pek1 = NULL;
int i;
for(i=0; i<langd; i++)
{
pek1 -> num = vek[i];
pek1 -> next = *temp;
*temp = pek1;
}
}
#包括
#包括
#包括
#包括“clabb2head.h”
int main()
{int-vek[5]={1,2,3,4,5};
int langd=sizeof(vek)/sizeof(vek[0]);
printf(“%d”,langd);
talstrul*温度=malloc(sizeof(talstrul));
talstrul*pek1=NULL;
int i;
for(i=0;i num=vek[i];
pek1->next=*温度;
*温度=pek1;
}
}
temp
属于talstrul*
talstrul *temp = malloc(sizeof(talstrul));
您正在尝试分配给下一个节点,该节点的类型为node*
struct node *next;
进一步
pek1 -> next = *temp;
取消引用temp
,生成talstrul
。不应取消引用指针
编译器对出现的问题给出了很好的解释。
temp
属于talstrul*
talstrul *temp = malloc(sizeof(talstrul));
您正在尝试分配给下一个节点,该节点的类型为node*
struct node *next;
进一步
pek1 -> next = *temp;
取消引用temp
,生成talstrul
。不应取消引用指针
编译器很好地解释了出错的原因。代码的另一个问题:
pek1被分配为NULL。但是您尝试分配pek1->num和pek1->next。您应该首先为pek1分配内存。代码的另一个问题:
typedef struct node {
int num;
struct node *next;
} talstrul;
...
int vek[5] = {1,2,3,4,5};
int langd = sizeof(vek)/sizeof(vek[0]);
talstrul *pek1 = NULL;
int i;
for(i=0; i<langd; i++){
talstrul *temp = malloc(sizeof(talstrul));
temp -> num = vek[i];
temp -> next = pek1;
pek1 = temp;
}
pek1被分配给NULL。但是您尝试分配pek1->num和pek1->next。您应该首先为pek1分配内存。typedef struct node{
typedef struct node {
int num;
struct node *next;
} talstrul;
...
int vek[5] = {1,2,3,4,5};
int langd = sizeof(vek)/sizeof(vek[0]);
talstrul *pek1 = NULL;
int i;
for(i=0; i<langd; i++){
talstrul *temp = malloc(sizeof(talstrul));
temp -> num = vek[i];
temp -> next = pek1;
pek1 = temp;
}
int-num;
结构节点*下一步;
}距骨;
...
int-vek[5]={1,2,3,4,5};
int langd=sizeof(vek)/sizeof(vek[0]);
talstrul*pek1=NULL;
int i;
for(i=0;i num=vek[i];
温度->下一步=pek1;
pek1=温度;
}
typedef结构节点{
int-num;
结构节点*下一步;
}距骨;
...
int-vek[5]={1,2,3,4,5};
int langd=sizeof(vek)/sizeof(vek[0]);
talstrul*pek1=NULL;
int i;
for(i=0;i num=vek[i];
温度->下一步=pek1;
pek1=温度;
}
1)结构节点的定义在哪里?2)为什么您希望能够将类型为talstrul
的*temp
分配给类型为struct*
的pek1->next
节点的struct*
?还有更多,但让我们从这里开始。您需要将typedef struct
更改为typedef struct节点{
。正如人们指出的那样,还有其他错误。1)结构节点的定义在哪里?2)为什么您希望能够将类型为talstrul
的*temp
分配给类型为struct*
的pek1->next
的结构节点
?还有更多,但我们从这里开始。您需要更改typedef结构{
到typedef结构节点{
。正如人们所指出的,还有其他错误。