Passing a Structure to a Function



I've got a little program in which I want to pass a struct to a
function.
For this I use :
 ->  gtk_signal_connect (object, "event", my_function,
pointer_to_struct)
 ->  my_function (GtkWidget *widget, gpointer data)	

The problem is that, in my_function, I do not receive the good pointer
(even with the appropriate cast).
( I saw that Alex Roberts and Ewan Laurence did the same thing in gEdit
0.3.2 and it worked ! )

Does anybody knows why this is not working for me ?



Here's a piece of my code, hope it'll be helpful.

struct _MyStruct {
	GtkWidget *window;
	gint ID;
};
typedef struct _MyStruct MyStruct;

MyStruct *pstruct;


MyStruct * create_new_struct (void);
{
  MyStruct *StructToPass;

  StructToPass = (MyStruct *)g_malloc(sizeof(MyStruct));
  StructToPass->window = gtk_window_new (GTK_WINDOW_TOP_LEVEL);
  StructToPass->ID = 1;

  g_print ("pointer = %p \n", StructToPass);
		/* The program prints "0x8066d18" */

  gtk_signal_connect (GTK_OBJECT (StructToPass->window),
"focus_in_event",
                      GTK_SIGNAL_FUNC (my_function), StructToPass);

  return StructToPass;
}


void my_function (GtkWidget *widget, gpointer data)
{
  MyStruct *PassedStruct;

  PassedStruct = (MyStruct *) data;
  g_print ("Struct ID = %d \n", PassedStruct->ID); 
		/* Here, the program prints "0" and not "1" as expected */ 
  g_print ("pointer = %p \n", PassedStruct);
		/* The program prints "0x80877e8" and not "0x8066d18" as expected */
}


int main (int argc, char *argv[])
{
  gtk_init (&argc, &argv);

  pstruct = create_new_struct ();
  g_print ("pointer = %p \n", pstruct);
			/* The program prints "0x8066d18" */

  gtk_main ();
  return 0;

}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]