Re: Can't get text from entry



On Tue, 14 Dec 2004, César Leonardo Blum Silveira wrote:

Why doesn't the following piece of code work?

#include <gtk/gtk.h>

char *string=NULL;

void combo_change(GtkWidget *widget, gpointer data)
{
  gchar *selected;

  selected = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(widget)->entry));

  strcpy(string, selected);

Bang! You are copying to a NULL pointer, "string". At this point your program segfaults if you are lucky, or does something weird if you are unlucky. You need to learn some basic C, since GTK programming is built on C.

Hint: you can make an allocated copy of "selected" by doing

  string = g_strdup(selected);

Since this is dynamically allocated storage, you should g_free() string once you're done with it.

Allin Cottrell



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