Re: Transmit an array to a function



Hi Anthony

> Jean-Christophe Berthon wrote:
> >
> > I'm not sure about it but try this :
> > gtk_signal_connect (GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(function),
> > widget);
> > according that widget is the name of your array (GtkWidget *widget[20])
> > This should work, I think (thought I never tried it...)
>
> No it doesn't work. During the compilation I get it:
> warning: passing arg 4 of `gtk_signal_connect_object' from incompatible
> pointer type
> But, anyway the prog works even with this compilation error.
> I can avoid this error by using "(gpointer)widget" instead of "widget"
> In fact, it's not the point. The pb is that I can transmit widget[1]
> without any pb but I can't transmit the whole array in the same call.
> Idem if I want to transmit an array of char:
>
> char *caract[20];
> gtk_signal_connect
> (GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(function),(gpointer)carac);
> <-- won't work
> gtk_signal_connect
> (GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(function),(gpointer)carac[1]);
> <-- will work
> (Of course, I change the receiving function to sync with the call)
>
> So, I am really lost... It must exist a solution to transmit an array of
> any type, no?
>
> Anthony
>

This (Widget *[]) example won't work, because at the end of the funtion
this var is deleted.

What you try to do is to send an array of 20 GtkWidget* - pointers to the
funtion - am I right?

First allocate memory for your array.
Second put the pointer at the user_data field of the signal

Expl.:

GtkWidget** my_widget_array = g_new(GtkWidget*, 20);
gtk_signal_connect(.., .., .., my_widget_array);

In the funtion do

GtkWidget** my_widget_array = (GtkWidget**) user_data;
(or whatever your gpointer var is called)

my_widget_array[0..19]

!!!!!!!!!!!!
Don't forget to g_free(my_widget_array).
When it's not used anymore.
!!!!!!!!!!!!


A code fragment, I use very often (not with GtkWidget*):

GtkWidget** values = g_new(GtkWidget*, nbr_of_values + 1);
values[nbr_of_values] = NULL;

fill the array (not with NULL / 0) ...

gtk_signal_connect(.., .., .., values);

in the funtion do

GtkWidget** my_values = (GtkWidget**) user_data;

guint i = 0;
while (my_values[i]) {
    do something with the values
    i++;
}

!!!!!!!!!!!!
Don't forget to g_free(my_widget_array).
When it's not used anymore.
!!!!!!!!!!!!


Bye

Jan-Marek





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