Re: [gtk-list] Re: erratum adding rows to CList from file



Aaron Walker wrote:
> 
> thanks! it works :-) I was wondering why it was segfaulting when I cast line
> to gpointer :-)  Although, it seemed kinda awckward doing.
> 
> thanks again,
> Aaron
> 
> Martin Kestel wrote:
> 
> > oops, sorry! switched in the lines... leave the gpointer casting out:
> > here is the corrected version
> >
> > >>FILE *fp
> > >>char line[256];
> > >>if((fp = fopen("/tmp/output", "r")) == NULL)
> > >>{
> > >>    perror("/tmp/output");
> > >>    exit(1);
> > >>}
> > >>clist = gtk_clist_new_with_titles(1, titles);
> > >>gtk_signal_connect(GTK_OBJECT(clist), "select_row",
> > >>            GTK_SIGNAL_FUNC(clist_selection_made_cb), NULL);
> > >>gtk_clist_set_shadow_type (GTK_CLIST(clist), GTK_SHADOW_OUT);
> > >>gtk_box_pack_start(GTK_BOX(vbox), clist, TRUE, TRUE, 0);
> > >>gtk_widget_show(clist);
> > >>while((fgets(line, sizeof line, fp)) != NULL)
> > >>    gtk_clist_append(GTK_CLIST(clist), line);
> > >>...
> > >>
> > >>ok, the above code yields the gcc error:
> > >>passing arg 2 of 'gtk_clist_append' from incompatiple pointer type
> > >>
> > >>now I know that arg 2 needs to be a gchar *[], but even if I cast line
> > >>to gchar *, it still doesn't work.
> > >>
> > >>Any ideas?
> > >
> > What if you add some
> > char *pointer_to_entry;
> >
> > and then say
> > pointer_to_entry = (gchar *)malloc((int)strlen(line));
> > pointer_to_entry = (gchar *)&line;
> >
> > then appending should work:
> > gtk_clist_append(GTK_CLIST(clist), (gpointer) pointer_to_entry);
> >
> > gtk_clist_append(GTK_CLIST(clist), &pointer_to_entry);
> >
> > (adding simply the '&' does not help the gcc...)
> >
> > This works for me like that, but the interesting question (to the
> > chefs) is why it does not go the easy way you had tried at first?
> >

The problem is that the prototype for the function is:

gint gtk_clist_append (GtkCList *clist, gchar *text[]);
                                              ^^^^^^^
which is calling for an array of pointers which is different than a
pointer to an array, sometimes it's hard to visualize the difference.

So to use an array as a buffer you can do:

char line[256];
char *line_ptr[1];    /* subscript line_ptr to the 
		         number of columns in the clist */

line_ptr[0] = line;   /* point line_ptr at the buffer */
...
gtk_clist_append (GTK_CLIST(out_clist), line_ptr);

Just remember that if you have more than one column to make sure that
line_ptr corresponds with the right number of pointers, *line_ptr[2],
*line_ptr[3] etc. and that they each point at something valid.

Also you could check out the tutorial for the Clist, the example shows
it pretty well
with a two column clist.

                HTH,
                        Stephen



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