Re: Typecast error
- From: Havoc Pennington <hp redhat com>
- To: Dwight Tuinstra <tuinstra northnet org>
- Cc: CyborgHead netscape net, gtk-list gnome org
- Subject: Re: Typecast error
- Date: 13 Mar 2001 22:33:45 -0500
Dwight Tuinstra <tuinstra northnet org> writes:
> > This program compiles and creates an executable which works
> > correctly, ie. ithe int is successfully passed to the function,
> > however I get this warning, which I would like to fix
> >
> > gui.h:873: cannot convert `int' to `void *' for argument `4' to
> > `gtk_signal_connect (GtkObject *, const gchar *, void (*) (), void
> > *)'
> >
The warning here is entirely justified; what you're doing is not
correct, because sizeof(int) != sizeof(void*) on many platforms.
So the lesson is, take warnings seriously, don't just insert
casts. ;-)
> It wants a pointer to something, so, we give it one:
>
> gint dummy_int = 1;
> gtk_signal_connect( GTK_OBJECT( robot_name[1] ), "clicked",
> GTK_SIGNAL_FUNC( display_info ), &dummy_int );
>
No! Once you're in the callback, dummy_int will no longer exist;
you'll have a pointer to random memory.
The correct, portable, no-warnings way to write this code is:
void
display_info (GtkWidget *widget, gpointer data)
{
gint i;
i = GPOINTER_TO_INT (data);
cout << i << endl;
}
...
gtk_signal_connect (GTK_OBJECT (robot_name[1]), "clicked",
GTK_SIGNAL_FUNC (display_info ),
GINT_TO_POINTER (42));
Havoc
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]