It's because of how C uses pointers and references. You can only pass things by value in C, so when you call CreateLabel(), you are creating two new lables, but the changes are never reflected in your original pointers to l1 and l2. The following is a fixed version of your code:
Code:
#include <gtk/gtk.h>
void CreateLabel(GtkWidget **label1,GtkWidget **label2,GtkWidget *box)
{
*label1 = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL(*label1), "Test1");
gtk_box_pack_start (GTK_BOX (box), *label1,FALSE,FALSE, 0);
gtk_widget_show (*label1);
*label2 = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL(*label2),"Test2");
gtk_box_pack_start (GTK_BOX (box), *label2,FALSE,FALSE, 0);
gtk_widget_show (*label2);
gtk_label_set_markup (GTK_LABEL(*label2),"Test3");
}
int main (int argc, char *argv[])
{
GtkWidget *window, *box1, *l1, *l2;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
box1 = gtk_hbox_new (FALSE, 0);
CreateLabel(&l1,&l2,box1);
gtk_label_set_markup (GTK_LABEL(l2),"Test4");
gtk_container_add (GTK_CONTAINER (window), box1);
gtk_widget_show_all (window);
gtk_main();
return 0;
}
Notice that you have to send l1 and l2 as pointers to the pointers. This is because the location of those pointers change in the function. For the changes to be reflected outside of the CreateLabel() function, you have to send references to them, and then dereference in the function. Notice that you do not have to do this with box1 since the location of the pointer will not change in CreateLabel().
In your example, two labels are created in CreateLabel() and added to box, but they are lost forever since the new location of the pointers are not reflected in l1 and l2. Does that make sense?
There is a great tutorial that I read when I was learning C, which may be of some help:
http://home.netcom.com/~tjensen/ptr/pointers.htm