0

I am working with a C code base that uses an API function (https://developer.gnome.org/glib/stable/glib-Hash-Tables.html#g-hash-table-size) that returns a guint. I needed to pass this along downstream as an int. I am wondering if there's any way to do this?

I searched the docs and Google but came up empty.

user1836155
  • 858
  • 14
  • 29
  • Possible duplicate of [converting an array of characters to a const gchar\*](https://stackoverflow.com/questions/2800310/converting-an-array-of-characters-to-a-const-gchar) – ptomato Oct 02 '17 at 02:55

1 Answers1

4

The mere existence of these typedefs baffles me. Check 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;

So no conversion is needed between unsigned int and guint, they are the same type.

The usual warnings about converting between unsigned int and int apply.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    https://stackoverflow.com/questions/1819561/why-does-glib-redefine-types?rq=1#comment1709760_1819582 is the most likely-sounding guess I've seen as to why this is – ptomato Oct 02 '17 at 02:55
  • 1
    https://stackoverflow.com/questions/1819561/why-does-glib-redefine-types?rq=1#comment1709760_1819582 is not a guess, it’s a correct fact. – Philip Withnall Oct 02 '17 at 07:58
  • 1
    That other SO article might be an explanation for the existence of `gint32` and similar; but so far as I can tell `gint` is a platform-defined `int` on all platforms; similarly other types without specific sizes. So what purpose do they serve? The existence of `gint`, `gchar`, etc., always struck me as an affectation. – Kevin Boone Oct 02 '17 at 11:18
  • Once they had a dozen types like `gint32` I think it was honestly just done for consistency. Fast forward a few decades its a little annoying but it isn't like `gint32` and friends can be dropped without breaking API. – TingPing Oct 03 '17 at 17:39