添加自定义结构GSList与油嘴 [英] Adding custom structure to GSList with Glib

查看:418
本文介绍了添加自定义结构GSList与油嘴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个结构添加到与功能g_slist_append(列表,和放大器;结构)的单链表。这似乎工作(它的加入指针),但是我似乎无法找到一种方法读取链表时查看该结构中的元素。

I'm trying to add a structure to a singly linked list with the function g_slist_append(list, &structure). This seems to work (it's adding the pointer), however I can't seem to find a way to view the elements in the structure when reading the linked list.

我的结构是这样的:

 struct customstruct
 {
   int var1;
   int var2;
   char *string_1;
 }

然后,我做一个列表: GSList *名单= NULL;

然后,我追加结构的一个实例是这样的:

Then, I append one instance of the structure like this:

 struct customstruct list_entry;
 list_entry.var1 = 1;
 list_entry.var2 = 2;
 list_entry.string_1 = "String";

 list = g_slist_append(list, &entry);

 printf("Entry var1 = %d\n", list->data->var1);

由于 VAR1 无法找到(在一些会员的要求不是一个结构或联合)。

That last line fails because var1 can't be found (request for member in something not a struct or union).

我想我需要它转换为正确的类型,但我不知道怎么办。有人吗?

I think I need to cast it to the right type but I don't know how. Anyone?

推荐答案

我猜数据的成员 GSList 结构是一个无效指针,即可以指向任何东西,但没有任何其他类型信息的指针。

I'm guessing the data member of the GSList structure is a void pointer, i.e. a pointer that can point to anything but doesn't have any other type info.

这意味着你必须使用类型强制转换:

This means you have to use type-casting:

((struct customstruct *) list->data)->var1

这篇关于添加自定义结构GSList与油嘴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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