Author Topic: C string with numbers for a label  (Read 10793 times)

0 Members and 4 Guests are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
C string with numbers for a label
« 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 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!
« Last Edit: October 02, 2011, 07:24:29 am by ephan »

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: C string with numbers for a label
« Reply #1 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);
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C string with numbers for a label
« Reply #2 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!