C programming pointer conversions with GTK

276 Views Asked by At

It appears that in GTK you hold a reference to an object, for example like an GtkEntry, but you hold it with a pointer to a GtkWidget. For example

GtkWidget* pointer = gtk_entry_new();

Then when you want to do something like set the text of that entry, you have to call a function and do something with that pointer.

gtk_entry_set_text(GTK_ENTRY(pointer), "hello");

A few questions:

  1. What is the "GTK_ENTRY()" thing? Is it a function call or typecast?
  2. Why is it in all Capital letters, why not do something like (GtkEntry*)pointer?
  3. Why even do this? Why not return a GtkEntry pointer when you create a new Entry?
1

There are 1 best solutions below

4
On BEST ANSWER

GTK_ENTRY is a macro, behaving like a function. It casts its argument to GtkEntry*, but it may - depending on macros like NDEBUG - do additional check to verify that provided argument may be cast.

It is in all CAPITAL because that is the general convention in C for macros.

The function gtk_entry_new returns GtkWidget* and not GtkEntry* more functions in GTK operates on GtkWidget*, and C language does not provide inheritance so the conversion cannot be automatic.