简体   繁体   English

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

[英]C cast fails: cannot cast from void* to a C struct

Code : 代码:

#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();
}      

Error : Cannot cast from void* to singlylist. 错误:无法从void *转换为singlylist。

Question : I cannot figure out the reason behind the error. 问题:我无法弄清楚错误背后的原因。 Can anyone explain me what error it is ? 任何人都可以解释一下它是什么错误吗?

malloc returns a [void] pointer, 'struct singlylist' is not a pointer at all. malloc返回一个[void]指针,'struct singlylist'根本不是指针。

I'm a little rusty in C, but that should work: 我在C中有点生疏,但这应该有效:

typedef struct singlylist *List;

L = (List) malloc(sizeof(*L));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM