Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Makalah
  • Ke-NU-an
  • Kabar
  • Search
Menu

Frequently Used Macros on Glib

Posted on April 8, 2012

glib defines a number of familiar macros used in many C programs, shown in Figure
2-1. All of these should be self-explanatory. MIN()/MAX() return the smaller or larger of their arguments. ABS() returns the absolute value of its argument. CLAMP(x, low, high) means x, unless x is outside the range [low, high]; if x is below the range, low is returned; if x is above the range, high is returned. In addition to the macros shown in Figure 2-1, TRUE/FALSE/NULL are defined as the usual 1/0/((void*)0).
[code lang=”cpp”]
#include <glib.h>

MAX( a , b ); MIN( a , b ); ABS( x );
CLAMP( x , low , high );
[/code]
Figure 2-1. Familiar C Macros

There are also many macros unique to glib, such as the portable gpointer-to-gint and gpointer-to-guint conversions shown in Figure 2-2.

Most of glib’s data structures are designed to store a gpointer. If you want to store pointers to dynamically allocated objects, this is the right thing. However, sometimes you want to store a simple list of integers without having to dynamically allocate them. Though the C standard does not strictly guarantee it, it is possible to store a gint or guint in a gpointer variable on the wide range of platforms glib has been ported to; in some cases, an intermediate cast is required. The macros in Figure 2-2 abstract the presence of the cast.

Here’s an example:
[code lang=”cpp”]
gint my_int;
gpointer my_pointer;

my_int = 5;
my_pointer = GINT_TO_POINTER(my_int);
printf("We are storing %d\n", GPOINTER_TO_INT(my_pointer));
[/code]

Be careful, though; these macros allow you to store an integer in a pointer, but storing a pointer in an integer will not work. To do that portably, you must store the pointer in a long. (It’s undoubtedly a bad idea to do so, however.)
[code lang=”cpp”]
#include <glib.h>

GINT_TO_POINTER(p); GPOINTER_TO_INT(p); GUINT_TO_POINTER(p); GPOINTER_TO_UINT(p);
[/code]

Figure 2-2. Macros for storing integers in pointers

Source: GTK Gnome Application Development

Terbaru

  • Profil Farida Farichah, Wakil Menteri Koperasi Kabinet Merah Putih Reshuffle 17 September 2025
  • Ini Info Terbaru Pencairan BSU BPJS Ketenagakerjaan 2025!
  • Cara Reset Printer Epson L3110 2025
  • WhatsApp Tiba-tiba Keluar dan Meminta Verifikasi: Apa yang Harus Dilakukan?
  • Bisakah Saldo BNI Kamu Nol? Fakta dan Cara Mengatasinya
  • Inilah Tanda-tanda Chat Audio di Grup WhatsApp Sudah Disadap
  • Cara Mengatasi Tidak Bisa Live Instagram Karena Tidak Memenuhi Syarat
  • 7 Spek Laptop yang Ideal untuk Coding & Ngoding Web/App
  • Keuntungan dan Kerugian Menggunakan PayPal: Panduan Lengkap
  • Cara Menggunakan Stellarium Web
  • Cara Menghapus Data KTP Pribadi di Pinjol yang Belum Lunas
  • Cara Mengganti Nomor TikTok yang Tidak Aktif atau Hilang Tanpa Verifikasi
  • Cara Menggunakan BCA PayLater Terbaru 2025
  • Cara Mendapatkan IMPoint Indosat IM3 Ooredoo Gratis via MyIM3
  • Apa Arti TikTok ‘Shared With You’?
  • Cara Menghapus Data KTP di Pinjol: Panduan Lengkap
  • Cara Download WhatsApp GB Terbaru 2025 – Fitur Lengkap & Aman
  • Review WhatsApp Beta: Apakah Aman? Cara Instal dan Cara Keluar
  • Bebong: Makna, Asal Usul, dan Penggunaan dalam Bahasa Indonesia
  • Spinjam dan Spaylater: Apa yang Terjadi Jika Terlambat Membayar dan Bisakah Meminjam Lagi?
  • Cara Download dan Menonton Dood Stream Tanpa Iklan – Doods Pro
  • Cara Menghentikan dan Mengatasi Pinjol Ilegal
  • Kode Bank BRI untuk Transfer ke PayPal
  • Cara Menyadap WhatsApp Tanpa Aplikasi dan Kode QR
  • Apa yang Terjadi Jika Telat Bayar Shopee PayLater?
  • Telat Bayar Listrik 1 Hari: Apa yang Terjadi?
  • Cara Mengunduh Foto Profil WhatsApp Teman di Android, iPhone, dan PC/Mac
  • Rekomendasi Aplikasi Edit Foto Ringan Terbaik untuk PC Windows dan macOS
  • Cara Membeli Diamond Mobile Legends Menggunakan Pulsa Telkomsel
  • Tutorial Menggunakan Aplikasi Dana: Cara Top Up Dana dengan Mudah, Cepat, dan Murah untuk Pemula
  • Profil Farida Farichah, Wakil Menteri Koperasi Kabinet Merah Putih Reshuffle 17 September 2025
  • Ini Info Terbaru Pencairan BSU BPJS Ketenagakerjaan 2025!
  • Cara Reset Printer Epson L3110 2025

©2025 emka.web.id | Design: Newspaperly WordPress Theme