C结构为void *指针 [英] C struct to void* pointer

查看:127
本文介绍了C结构为void *指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构定义为:

typedef struct {
   int type;
   void* info;
} Data;

然后我有我想要分配到的void *使用下面的函数其他几个结构:

and then i have several other structs that i want to assign to the void* using the following function:

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

当我编译这个它会发出警告

When i compile this it gives a warning

warning: assignment from incompatible pointer type

我试着投变量在插入到(void *的),但没有奏效。

i tried to cast the variable in the insert to (void*) but didn't work

insert_data(1, (void *) variable_of_type_Struct#);

如何才能摆脱这种警告?

How can i get rid of this warning?

感谢

推荐答案

在传递的结构,而不是它的一个副本(即不按值传递)地址:

Pass in the address of the struct, not a copy of it (i.e. not passed by value):

insert_data(1, &variable_of_type_Struct);

这篇关于C结构为void *指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆