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

时间:2013-08-19 04:34:36

标签: c++ c list dsa

代码:

#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 *转换为singlylist。

问题: 我无法弄清楚错误背后的原因。任何人都可以解释一下它是什么错误吗?

1 个答案:

答案 0 :(得分:2)

malloc返回一个[void]指针,'struct singlylist'根本不是指针。

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

typedef struct singlylist *List;

L = (List) malloc(sizeof(*L));
相关问题