RE: GTK Casting Problem



Hello,
	There are some points suspicious I think. Firstly, you can't copy a
string with the statement "*label = (gpointer)"Button ";", for the var label
has not been malloced yet, and a string can't be copied like this, use
strcpy/strncpy instead. Secondly, the strcat will take two vars of the type
"char *", so you can't concat a string and an integer, before you do that,
you can transform the integer into string.

---------------------------------------
Best Wishes!
 
Zhang Hui
Email: Hui003 Zhang samsung com
Phone: 86-25-58748787-8215 
Samsung Electronics(China) R&D Center
---------------------------------------
-----Original Message-----
From: gtk-list-bounces gnome org [mailto:gtk-list-bounces gnome org] On
Behalf Of Chris
Sent: Wednesday, November 29, 2006 8:27 AM
To: gtklist
Subject: GTK Casting Problem

Hello,

I am new to gtk.
To learn the language I am writing simple
programs.
I want to be able to create 3 buttons, named
Button 1, Button 2 & Button 3.
If I explicitly initialize them it works.
When I pull the Initilize into a for loop
things start to go wrong.
I think that I am either not using the proper type
or not properly casting them.
The result is a segmentation error when I compile
which I think means a pointer is not correct.
If anyone knows how to fix this issue, it would be
greatly appreciated.
I have placed in bold the two sections
that I think apply to the problem.

My code is as follows:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>

static void button_cb (GtkWidget *widget, gpointer data)
    {
        g_print ("Hello - %s was pressed\n", (gchar *) data);
    }

static void delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer 
data)
    {
        g_print ("A delete event has occurred\n");
        gtk_main_quit();
    }

int main( int   argc,
          char *argv[] )
    {
        GtkWidget *window;
        GtkWidget *button;
        GtkWidget *box1;

*        gpointer label;
        gint i;
*
        gtk_init (&argc, &argv);
   
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title (GTK_WINDOW (window), "Test Program");
        gtk_container_set_border_width (GTK_CONTAINER (window), 50);
        g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK 
(delete_event_cb), NULL);
     
        box1 = gtk_hbox_new (FALSE, 0);
        gtk_container_add (GTK_CONTAINER (window), box1);

        for (i=1; i<=3; i=i+1)
          {

            *label = (gpointer)"Button ";
            strcat (label, (gpointer) i);
            button = gtk_button_new_with_label ((gchar *)label);
            g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK 
(button_cb), label);*

            gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
            gtk_widget_show (button);
          }

        gtk_widget_show (box1);
        gtk_widget_show (window);
   
        gtk_main ();
   
        return 0;
    }
 



_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list





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