Re: Cast from gchar* to int?



On 24 Sep 2002, William L. Thomson Jr. wrote:

Neither of these work, and both return the exact same result, 0.
Now the 0 is not the one that I initialized gint i to, but the value that
is returned from passing the const gchar *num to the atoi function.

So if I start with gint i = 4, it will still output 0;

const gchar *num = gtk_entry_get_text(GTK_ENTRY(contact_num_entry));
gint i = 0;
i = atoi(num);
cout << i << endl;

If the string returned by gtk_entry_get_text() is valid numeric input 
I can't see any reason why atoi shouldn't convert it to an int.  A 
couple of suggestions: (i) Can you really mix C++ and C in this way? 
("cout" along with the gtk calls).  (ii) Arrange to have your program 
print the string returned by gtk_entry_get_text() printed to stderr 
before the conversion -- then perhaps you'll see why the conversion is 
failing.

const gchar *num = gtk_entry_get_text(GTK_ENTRY(contact_num_entry));
gint i = 0;

fprintf(stderr, "Got supposedly numeric string '%s'\n", num);
i = atoi(num);
fprintf(stderr, "Int conversion of num = %d\n", i);

Allin Cottrell.








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