Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on October 02, 2011, 07:24:12 am

Title: C string with numbers for a label
Post by: Munchor on October 02, 2011, 07:24:12 am
I am using GTK+ to create a label with numbers on it:

Code: [Select]
time_text = gtk_label_new("Time: %i:%i:%i", hours, minutes, seconds);
The gtk_label_new (http://developer.gnome.org/gtk/2.24/GtkLabel.html#gtk-label-new) function takes a const gchar *str as argument. However, when compiling the code, I get:

Quote from: GCC
error: too many arguments to function ‘gtk_label_new’

This is because I am passing four arguments to gtk_label_new.

So I need to pass only one string, but I need the numbers included (hours, minutes, seconds).

Any idea? Thanks!
Title: Re: C string with numbers for a label
Post by: calc84maniac on October 02, 2011, 12:26:19 pm
Try this:
Code: [Select]
// I chose 20 arbitrarily as a maximum buffer size, you can have a lower character limit if you want
char temp_text[20];
// Using %02i instead of %i because you probably want 2 characters padded with 0 on the left
snprintf(temp_text, sizeof(temp_text), "Time: %02i:%02i:%02i", hours, minutes, seconds);
// Now call with one argument like it wants
time_text = gtk_label_new(temp_text);
Title: Re: C string with numbers for a label
Post by: Munchor on October 02, 2011, 12:33:56 pm
It worked perfectly, I found a way that converts each number to a char*, but using snprintf(); is much cleaner, thanks!