【发布时间】:2014-08-28 01:10:25
【问题描述】:
您好,考虑到没有 GFLOAT_TO_POINTER 宏方法,我想知道是否可以将浮点数作为键存储到 GhashTable 中。
我正在学习 IBM http://www.ibm.com/developerworks/linux/tutorials/l-glib/section5.html 在网上找到的教程,但我似乎找不到使用浮点数作为键的方法。
任何帮助将非常感谢!
typedef struct Settings settings;
typedef struct Preset preset;
struct Gnomeradio_Settings
{
GList *presets;
};
struct Preset
{
gchar *name;
gfloat freq;
};
我想将 settings.presets 列表中的所有频率作为 GHashTable 中的键
GHashTable *hash;
GList *node;
hash = g_hash_table_new (g_double_hash, g_double_equal);
for (node = settings.presets; node; node = node->next) {
preset *ps;
gfloat *f;
ps = (preset *) node->data;
f = g_malloc0 (sizeof (gfloat));
*f = ps->freq;
g_hash_table_insert (hash, f, NULL);
}
void printf_key (gpointer key, gpointer value, gpointer user_data)
{
printf("\t%s\n", (char *) key);
}
void printf_hash_table (GHashTable* hash_table)
{
g_hash_table_foreach (hash_table, printf_key, NULL);
}
printf_hash_table (hash);
但没有成功!
这个印刷品:
���B
ff�B
ff�B
���B
ff�B
f��B
f��B
���B
33�B
ff�B
�L�B
���B
�̲B
【问题讨论】:
-
看到这个answer。