Re: gtk_signal_connect() and passing parameters
- From: Markus Lausser <sgop users sourceforge net>
- To: gtk-app-devel-list gnome org
- Subject: Re: gtk_signal_connect() and passing parameters
- Date: Wed, 16 May 2001 02:45:26 +0200
On Wed, May 16, 2001 at 10:22:17AM +1000 Mazur Przemyslaw wrote:
Hello,
I'm having trouble passing any kind of parameters to
my call back functions. For example:
int i = 1456;
...
gtk_signal_connect(GTK_OBJECT(button1),
"button_press_event",
GTK_SIGNAL_FUNC(button1_callback), (gpointer) &i);
...
void button1_callback(GtkWidget *widget, gpointer
data)
{
g_print("i: %d\n", *((int *)data));
}
Quite simply, I expect to see g_print() print 1456 as
the value of i. It does not. Instead, it prints 4.
Even more strange, if I double click on the button
(which is a checkbox button), it prints 5 instead.
This leads me to believe that what I'm getting via the
data parameter is something other than the address of
my i variable. I tried passing the value of i instead
(removing the &), and changing to g_print("i: %d\n",
data); but this gave me another bizzare value instead.
I've had two experienced programmers (although not
experienced in GTK) look at this and both are stuck
for a solution.
Anyone have ideas? I'm starting to think there's some
bug in my version of GTK.
What you do is: you pass the pointer to a local variable to the callback
function. Thats not a good idea.
instead you have to pass the _value_ itself:
GTK_SIGNAL_FUNC(button1_callback), (gpointer) i);
and access it with:
g_print("i: %d\n", (int)data);
Note: if you cast an int to a pointer you should be sure, that
sizeof(gpointer) >= sizeof(int)
Btw. i dont know any architecture where this is false, but that doesnt mean,
that there isnt any.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]