C强制转换失败:无法从void*强制转换为C结构

C cast fails: cannot cast from void* to a C struct

本文关键字:转换 结构 void 失败      更新时间:2023-10-16

代码:

#include<stdio.h>
#include<malloc.h>
#include<conio.h>
typedef struct singlylist *nodeptr;
typedef struct singlylist *position;
struct singlylist
{
  int x;
  position next;
}

typedef struct singlylist List;
List L;
int isempty(List A)
{
 return(A.next==NULL);
}
void create()
{
 L=(struct singlylist)malloc(sizeof(struct singlylist));
 L.next=NULL;
}
main()
{
 create();
 if(isempty(L))
 puts("Empty list !!!");
 getch();
}      

错误:无法从void*强制转换为singlelist。

问题:我想不出这个错误背后的原因。有人能解释一下这是什么错误吗?

malloc返回一个〔void〕指针,‘struct-singlylist’根本不是指针。

我对C有点生疏,但这应该有效:

typedef struct singlylist *List;
L = (List) malloc(sizeof(*L));