0

I'm trying to get a pretty simple UI working with GTK+. I think I'm running into some issues pertaining to C and pass-by-reference.

Here is some code from my main:

...
GtkWidget *controlArea;
GtkWidget *sendButton;
GtkWidget *createAccountButton;
GtkWidget *textBox;
...
controlArea = create_control_area(sendButton, createAccountButton, textBox);
...
g_signal_connect(sendButton, GTK_CALLBACK(send), textBox);
g_signal_connect(createAccountButton, GTK_CALLBACK(createAccount), NULL);
...

And here is some code from the create_control_area() function:

...
textBox = gtk_text_view_new();
...
sendButton = gtk_button_new_with_label("Send Message");
createAccount = gtk_button_new_with_label("Create Account");
...

The Text View and Buttons are attached to a widget which is then returned by that function. My two callback methods both exist but are currently empty. When I run the program, I reach a segfault on the g_signal_connect() calls (either of them will do it). I tested in gdb and it seemed to indicate all three widgets, textBox, sendButton, and createAccount were valid, but I'm not sure. Any ideas?

tux3
  • 7,171
  • 6
  • 39
  • 51
quiet_laika
  • 174
  • 10
  • What do you mean by: "valid"? If you mean a value != 0 they can still contain garbage ... – smagnan Apr 16 '15 at 13:46
  • Hmmm good point. They pointed to seemingly sane (not 100% sure what the criteria are) locations, and they appear properly on the UI if I don't give them a callback function. How can I test if the pointers inside of main are still proper? – quiet_laika Apr 16 '15 at 13:48
  • C doesn't know of _"passing by reference"_, it passes everything by value. The value you pass can either be the value itself (an int, a char, or some other type), or a memory address to a value (a pointer, which is passed by value, too) – Elias Van Ootegem Apr 16 '15 at 13:58

1 Answers1

2

The problem you have is that the pointers you pass to the function are passed by value, i.e. they are copied and changing the local copies of the pointers inside the function will of course not change the original pointers.

The solution is to pass the pointers by reference, which unfortunately isn't supported by C but can be emulated using a pointer. So you have to pass a pointer to the pointer by using the address-of operator & when calling the function:

controlArea = create_control_area(&sendButton, &createAccountButton, &textBox);

You, of course, have to modify the function to take pointers to pointers and use the dereference operator * when assigning to the pointers

*textBox = gtk_text_view_new();
...
*sendButton = gtk_button_new_with_label("Send Message");
*createAccount = gtk_button_new_with_label("Create Account");
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you! This was exactly what I needed, I just couldn't think through it all the way in my head. – quiet_laika Apr 16 '15 at 13:55
  • I'd say the other way around: it is the reference that is emulated by using a pointer of a pointer. – ntd Apr 17 '15 at 18:25