GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing and creating labels. Returning a pointer.

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
tblue
Familiar Face


Joined: 21 Mar 2007
Posts: 13

PostPosted: Tue Jul 10, 2007 8:22 am    Post subject: Passing and creating labels. Returning a pointer. Reply with quote

I created a function to add two labels inside a box.
The box is created outside the function, the labels are created inside.
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13

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");
}


I call it like this...

Code: (Plaintext)
1
2
3
CreateLabel(l1,l2,box1);
    gtk_label_set_markup (GTK_LABEL(l2),"Test4");

My question is why does It show Test1 and Test3. Why once outside the function can I not edit the l2 label.

Thankyou

/T
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Tue Jul 10, 2007 11:55 am    Post subject: Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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
Back to top
tblue
Familiar Face


Joined: 21 Mar 2007
Posts: 13

PostPosted: Tue Jul 10, 2007 1:21 pm    Post subject: Reply with quote

Much appreciated OpenlDev, thankyou.
/T
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP