razz
razz

Reputation: 10110

GTK+ 3.0 warning: missing initializer for field ‘padding’ of ‘GActionEntry’

I'm going through this tutorial from gnome gtk+ website, I'm new to gtk+ and not that advanced in c. I'm having some problems with this bit of code:

   static GActionEntry app_entries[] = {
        {"preferences", preferences_activated, NULL, NULL, NULL},
        {"quit", quit_activated, NULL, NULL, NULL}
    };

When i compile it with -Wextra -Wall -g -O2 -pedantic-errors, it throws the missing initializer warning:

gcc -c -o app.o -Wextra -Wall -O2 -pedantic-errors -g `pkg-config --cflags gtk+-3.0` app.c
app.c:38:2: warning: missing initializer for field ‘padding’ of ‘GActionEntry’ [-Wmissing-field-initializers]
  { "preferences", preferences_activated, NULL, NULL, NULL },
  ^
In file included from /usr/include/glib-2.0/gio/gio.h:31:0,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from app.c:1:
/usr/include/glib-2.0/gio/gactionmap.h:72:9: note: ‘padding’ declared here
   gsize padding[3];
         ^
app.c:39:2: warning: missing initializer for field ‘padding’ of ‘GActionEntry’ [-Wmissing-field-initializers]
  { "quit", quit_activated, NULL, NULL, NULL }
  ^
In file included from /usr/include/glib-2.0/gio/gio.h:31:0,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from app.c:1:
/usr/include/glib-2.0/gio/gactionmap.h:72:9: note: ‘padding’ declared here
   gsize padding[3];
         ^

Why am i getting this warning? and how can i fix it?

Thanks in advance

Upvotes: 1

Views: 817

Answers (2)

elboulangero
elboulangero

Reputation: 872

As Brad. S noted in his reply, this is because GActionEntry has a private "padding" field, that is not initialized in your code. GCC warns you about that.

However, you can trick GCC by using designated initializers. Have a look at GCC Warning Options.

-Wmissing-field-initializers

This option does not warn about designated initializers, so the following modification does not trigger a warning:

     struct s { int f, g, h; };
     struct s x = { .f = 3, .g = 4 };

Designated initializers are useful if you want to initialize only a few fields of a structure. Un-initialized fields will be set to zero, according to the C99 standard.

(C99 section 6.7.8.19) "If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration."

By using designated initializers, your code could look like that:

static GActionEntry app_entries[] = {
    { .name = "preferences", .activate = preferences_activated },
    { .name = "quit",        .activate = quit_activated }
};

Such code won't raise warnings from GCC.

Of course, GCC can't distinguish between the fields you omit on purpose, and the fields you really forgot to initialize. By using this syntax, you'll get no more warnings, you're on your own.

Also, note that designated initializers don't exist in C prior to C99.

Upvotes: 1

Brad S.
Brad S.

Reputation: 337

Looking at the header file /usr/include/glib-2.0/gio/gactionmap.h , we find that GActionEntry is defined as follows:

struct _GActionEntry
{
    const gchar *name;
    void (*activate) (GSimpleAction *action, GVariant *parameter, gpointer user_data);
    const gchar *parameter_type;
    const gchar *state;
    void (* change_state) (GSimpleAction *action, GVariant *value, gpointer user_data);
    /*< private >*/
    gsize padding[3];
};

typedef struct _GActionEntry GActionEntry;

See that last member of the struct? You need to supply an initializer for it if you are going to have warnings enabled for 'missing initializer'.

Try something like this:

static GActionEntry app_entries[] = {
    {"preferences", preferences_activated, NULL, NULL, NULL, {0,0,0}},
    {"quit", quit_activated, NULL, NULL, NULL, {0,0,0}}
};

Upvotes: 1

Related Questions