Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 简单链表代码中的错误_C_Pointers_Struct - Fatal编程技术网

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结构节点{
。正如人们所指出的,还有其他错误。