Re: Passing a gint to a label...



On Tue, Jun 27, 2000 at 10:03:05PM +0000, scherfa@fh-trier.de wrote:
> "G.Gabriele" wrote:
> > 
> > Hi all,
> > 
> > I need to put in a label a gint value...
> > 
> > void pass-help (gint value)
> > {
> >     gtk_label_set_text( GTK_LABEL(label),  value);
> > }
> > 
> > How could I do that ?
> >
> Maybe this way : 
> void pass-help (gint value)
> {
> gchar * buffer;
> buffer = (gchar *)g_malloc(sizeof(value));
> g_snprintf(buffer,sizeof(value),"%d",value);
> g_free(buffer);
> }

Using the sizeof(value) for deciding the buffer size is odd.
What if sizeof(int) is 4, but the value is 9999?  The string will
need 5 bytes -- "9999" plus the zero termination.  Instead try:

   gchar buffer [16];
   sprintf (buffer, "%d", value);
   gtk_label_set_text (GTK_LABEL(label), buffer);

because 16 chars is a nice power of 2 that should be big enough to hold
value an int can throw at it with room to spare.   if you're really
concerned about not using a fixed buffer:

   gchar * buffer = g_strdup_printf ("%d", value);
   gtk_label_set_text (GTK_LABEL(label), buffer);
   g_free (buffer);

cheers,
Charles




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