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

Re: [Sheflug] stupid C question



On Fri, Nov 03, 2000 at 01:59:52PM +0000, José Luis Gómez Dans wrote:
> Hi!
> 	This should be an easy one for seasoned C programmer. OK, I am
> using GTK+ mixed with Fortran 95 in a project. The GTK+ bit and the
> skeleton of the program is C, but I do use F95 routines to get the work
> done. Due to this and the fact that the F95 compiler is _scary_ to say
> the least (tends to corrupt some of the global variable space), I want
> to minimize the number of global variables. So, without using global
> variables (otherwise, solution would be easy!), let's start:
> 
> 	In a function (interface.c), I define several local variables. I
> then want to pass them on to event call-backs using
> gtk_signal_connect(). As an example
> 
> gtk_signal_connect (GTK_OBJECT (interferogram_polar), "clicked",
> 		      GTK_SIGNAL_FUNC (on_interogram_polar_clicked),
> 		      		      NULL) ;
> Where the NULL can be exchanged by a variable that will be passed on to
> on_interferogram_polar_clicked(). The latter is defined as
> void
> on_select_ampl_polaristaion_changed    (GtkEditable     *editable,
>                                         gpointer         user_data) ;
> and the NULL (or variable) goes in user_data. 
> 
> 	Let's assume that I pass an integer. I do this by having
> &my_int_var instead of the NULL in the gtk_signal_connect function. I
> then do some processing in the callback handler, and end up with a new
> integer value that needs to update the value of my_int_var in the first
> function. How can I set this value?
> 
> 	The problem is that user_data is a gpointer (==void *), so I
> cannot access it as I'd normally do with a pointer. This is illegal:
> *user_data = 9 ; (say)
> 
> 	I can set user_data to a value in memory, but as soon as the
> callback function dies, the value is cleared:
> user_data = &callback_int ;
> so the value in the main function isn't updated.
> 
> 	Graphically,
> 	
> 	main<------Update value------------|
> 	|                                  |
> 	|                                  |
> 	|----->callback->{change value}----|
> 
> 	It must be possible to do this without a global variable!!!
> 
> 	Thanks,
> 	José

You can have a second variable, which is a pointer to the type you
want, and cast your input value onto it. So, for example, 

	int	*ip_user_data = (int *) user_data;
	ip_user_data = 9;

should work. The variable ip_user_data points to the same place as
user_data, but it believes that the stuff there is an 'int' rather
than a 'void'.

A.D.

---------------------------------------------------------------------
Sheffield Linux User's Group - http://www.sheflug.co.uk
To unsubscribe from this list send mail to
- <sheflug-request [at] vuw.ac.nz> - with the word 
 "unsubscribe" in the body of the message. 

  GNU the choice of a complete generation.