I have a struct defined as:
typedef struct {
int type;
void* info;
} Data;
然后我有几个其他的结构体,我想使用以下函数将它们分配给void*:
Data* insert_data(int t, void* s)
{
Data * d = (Data*)malloc(sizeof(Data));
d->type = t;
d->info = s;
return d;
}
struct {
...
} Struct#;
然后我只需要调用:
insert_data(1, variable_of_type_Struct#);
我编译时收到了一个警告
warning: assignment from incompatible pointer type
我尝试将插入操作中的变量转换为(void*),但没有成功。
insert_data(1, (void *) variable_of_type_Struct#);
我该如何消除这个警告?
谢谢
malloc
的结果进行转换是不必要的。只需要写成Data * d = malloc(sizeof(Data));
即可。 - Matheus Moreira