문제

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.

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.

I'm a little rusty in C, but that should work:

typedef struct singlylist *List;

L = (List) malloc(sizeof(*L));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top