tags:

views:

181

answers:

3

Hii!, i'm programming in C/GTK+, but i don't understood yet why, when I call a function, like gtk_label_set_text (GTK_LABEL (label), "some text"); for example, i don't need to pass the reference of label pointer to the function. I learned that C pass all arguments as value, than, the function will don't affect the Widget label, in other scope. Thanks a lot, and sorry my bad english.

+1  A: 

You ARE passing a pointer. GTK_LABEL (label) is just a fancy macro that casts/checks the type of 'label'. But it works with pointers, you're passing a pointer to gtk_label_set_text.

Also, C passes everything by value. But it's the pointer you pass as value. The copy of the pointer still points to the same 'object', so gtk_label_set_text will manipulate the same object as the caller have.

nos
A: 

In C, you pass a pointer by reference ( int *pointer; myfunction(&pointer); ) if you need the function to modify the pointer, i.e. make the pointer point to a different memory address.

If you want to change the text in a GtkLabel, there is no need to change where the label pointer points. It already points at the right place - a struct which is probably somewhere on the heap. All you want to do is change the value of one of the fields of the struct to read "some text". And this is why the Gtk+ API specifies that in this case, you need to pass your label pointer by value.

tetromino
A: 

GTK_LABEL() is a macro that basically casts a generic GtkWidget pointer to a GtkLabel pointer. If you have a GtkWidget *foo which points to a GtkLabel object, GTK_LABEL(foo) just means more or less the same as (GtkLabel*)foo. (With added checks that the pointed-to object actually is a GtkLabel.) It doesn't mean the "label" in the GtkLabel widget, as you seem to think.

tml