GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Aug 25, 2016 2:56 pm

All times are UTC

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Addition problem with GTK
PostPosted: Mon Mar 03, 2008 1:45 am 
Familiar Face

Joined: Tue Feb 26, 2008 2:15 am
Posts: 12
I am at learning phase of C and GTK and i wrote following code in callbacks.c
but it get compiles without error but if i click a button (button1) the GNOME Bugbudddy comes

# include <config.h>

#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"

on_button1_clicked (GtkButton *button,
gpointer user_data)
GtkWidget * label = lookup_widget(GTK_WIDGET(button), "label1");
GtkWidget * entry1 = lookup_widget(GTK_WIDGET(button), "entry1");
GtkWidget * entry2 = lookup_widget(GTK_WIDGET(button), "entry2");
GtkWidget * entry3 = lookup_widget(GTK_WIDGET(button), "entry3");
gint numone = 0;
gint numtwo = 0;
numone = gtk_entry_get_text(GTK_ENTRY(entry1));
numtwo = gtk_entry_get_text(GTK_ENTRY(entry2));
gint ans = 0;
ans = numone + numtwo;
gtk_label_set_text(GTK_LABEL(label),"The solution is:");
I think the error is in this statement:

ans = numone + numtwo;

Thanks in advance...

 Post subject:
PostPosted: Tue Mar 11, 2008 3:59 am 
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Could you post all of the code please all you have there is a gtk button clicked event.

 Post subject:
PostPosted: Thu Mar 20, 2008 5:59 am 
Familiar Face

Joined: Wed Oct 03, 2007 12:28 pm
Posts: 25
Location: Virginia (Northern)
I think your problem is that:
gtk_entry_get_text() returns a pointer to a character string which contains the "text" value internal to the widget.

Therefore when it executes: numone = gtk_entry_get_text(GTK_ENTRY(entry1));
numone is actually an address, not the numeric value of the string.

Your ans = numone + numtwo simply adds two pointer values.

When you perform the gtk_entry_set_text(GTK_ENTRY(entry3),ans)
the value of "ans" is an address to some bogus memory location.

To do what you want to do (assuming that you want to stick with a GtkEntry widget (instead of a GtkSpinButton which is what I recommend) you will have to convert the string to a numerical value ( look at the function "strtol" ) to assign it to your gint values. Then after calculating the answer (ans) convert it back to a string using sprintf before using gtk_entry-set_text() which requires a string pointer.

(Note: to clarify, by "string I am referring to a char* or char array, NOT the C++ STL string class.)


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 4 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group