簡體   English   中英

C cast失敗:無法從void *轉換為C結構

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

代碼:

#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。

問題:我無法弄清楚錯誤背后的原因。 任何人都可以解釋一下它是什么錯誤嗎?

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

我在C中有點生疏,但這應該有效:

typedef struct singlylist *List;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM