C结构为void *指针

编程入门 行业动态 更新时间:2024-10-27 16:23:59
本文介绍了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 *指针

本文发布于:2023-07-29 08:24:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239049.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   结构   void

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!