Re: Combo append

This should really have gone to gtk-list gnome org or gtk-app-devel-list. This list is for discussion about development _of_ gtk, rather than development _with_ gtk.

Jano Crema wrote:

The object receive test1 e test2
  glist = g_list_append(glist, "test1");
  glist = g_list_append(glist, "test2");
  gtk_combo_set_popdown_strings( GTK_COMBO(combo), glist) ;

Why this not works, only appear test2 (two times)

  sprintf(empresa, "test1");
  glist = g_list_append(glist, empresa);

  sprintf(empresa, "test2");
  glist = g_list_append(glist, empresa);

  gtk_combo_set_popdown_strings( GTK_COMBO(combo), glist) ;

The sprintf calls copy the contents of the string into the buffer empresa -- they don't create new strings.

So the sequence in your second example is:
 put "test1" in empresa.
 append the address of empresa to the list
 now put "test2" in empresa (this overwrites the last value you set).
 append the address of empresa to the list again.

So you get two pointers to empresa in the list, which is why you see "test2" twice. If you are using format strings, you might be interested in the g_strdup_printf() function, which is like sprintf, but allocates the result on the heap.

After calling gtk_combo_set_popdown_strings, don't forget to free the strings and the list, or you will leak.


Email: james daa com au

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]