GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Jul 31, 2014 3:19 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Passing and creating labels. Returning a pointer.
PostPosted: Tue Jul 10, 2007 8:22 am 
Offline
Familiar Face

Joined: Wed Mar 21, 2007 1:02 pm
Posts: 16
I created a function to add two labels inside a box.
The box is created outside the function, the labels are created inside.
Code:
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:
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 10, 2007 11:55 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
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

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 10, 2007 1:21 pm 
Offline
Familiar Face

Joined: Wed Mar 21, 2007 1:02 pm
Posts: 16
Much appreciated OpenlDev, thankyou.
/T


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group