C强制转换失败:无法从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*强制转换为singlelist。
问题:我想不出这个错误背后的原因。有人能解释一下这是什么错误吗?
malloc返回一个〔void〕指针,‘struct-singlylist’根本不是指针。
我对C有点生疏,但这应该有效:
typedef struct singlylist *List;
L = (List) malloc(sizeof(*L));
相关文章:
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 从成员指针到整个结构/类的强制转换
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 将字符数组转换为时间结构
- 使用带有链表的堆栈数据结构将中缀转换为后缀
- i2d_X509_REQ_INFO无法正确转换req_info结构
- 在C++中将结构转换和存储为二进制
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- 转换包含向量 C++ 的语法和结构
- 正在转换结构数据的字节序
- 如何大致将 cpp 代码结构转换为 python
- 将结构 std::memcpy 转换为具有足够容量的 std::vector 是未定义的行为<char>吗?
- C++ 向下转换结构
- 尝试在C++中转换结构时出现奇怪的错误消息
- 向向下转换结构添加属性值会导致分段错误
- C++基于子类型动态强制转换结构
- 从字节缓冲区强制转换结构
- c++中的类型转换结构
- c++的类型转换结构指针指向类型值