【问题标题】:Type conversion between Glib types (e.g. guint) to standard C data types (e.g. int)Glib 类型(例如 guint)到标准 C 数据类型(例如 int)之间的类型转换
【发布时间】:2017-10-02 07:29:26
【问题描述】:

我正在使用一个使用返回 guint 的 API 函数 (https://developer.gnome.org/glib/stable/glib-Hash-Tables.html#g-hash-table-size) 的 C 代码库。我需要将其作为 int 传递给下游。我想知道有没有办法做到这一点?

我搜索了文档和谷歌,但没有找到。

【问题讨论】:

标签: c type-conversion glib


【解决方案1】:

这些 typedef 的存在让我感到困惑。检查glib/gtypes.h

typedef char   gchar;
typedef short  gshort;
typedef long   glong;
typedef int    gint;
typedef gint   gboolean;

typedef unsigned char   guchar;
typedef unsigned short  gushort;
typedef unsigned long   gulong;
typedef unsigned int    guint;

所以unsigned intguint之间不需要转换,它们是相同的类型。

适用于在 unsigned intint 之间转换的常见警告。

【讨论】:

  • stackoverflow.com/questions/1819561/… 是我所见过的关于为什么会这样的最有可能的猜测
  • stackoverflow.com/questions/1819561/… 不是猜测,而是正确的事实。
  • 其他 SO 文章可能是对 gint32 和类似存在的解释;但据我所知,gint 在所有平台上都是平台定义的int;类似的其他类型没有特定的大小。那么他们服务的目的是什么? gintgchar等的存在,总让我觉得做作。
  • 一旦他们有像gint32 这样的十几个类型,我认为老实说只是为了保持一致性。快进几十年它有点烦人,但它不像gint32 并且可以在不破坏 API 的情况下删除朋友。
猜你喜欢
  • 1970-01-01
  • 2020-10-22
  • 2012-06-19
  • 2016-06-25
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
相关资源
最近更新 更多