我試圖學習C並已經遇到了問題。 我認爲它微不足道,但我需要知道它。我已經寫:警告:格式不是字符串文字,參數類型沒有檢查
char *seconds_to_string (guint seconds)
{
long days, hours, minutes;
char *time = NULL;
const char *minutefmt;
const char *hourfmt;
const char *secondfmt;
days = seconds/(60 * 60 * 24);
hours = (seconds/(60 * 60));
minutes = (seconds/60) - ((days * 24 * 60) + (hours * 60));
seconds = seconds % 60;
minutefmt = ngettext ("%ld minute", "%ld minutes", minutes);
hourfmt = ngettext ("%ld hour", "%ld hours", hours);
secondfmt = ngettext ("%ld second", "%ld seconds", seconds);
minutefmt = ngettext ("%ld minute", "%ld minutes", minutes);
hourfmt = ngettext ("%ld hour", "%ld hours", hours);
secondfmt = ngettext ("%ld second", "%ld seconds", seconds);
char *fmt;
/* Translators: the format is "X hours X minutes X seconds" */
fmt = g_strdup_printf (_("%s %s %s"), hourfmt, minutefmt, secondfmt);
time = g_strdup_printf (fmt, hours, minutes, seconds);
g_free (fmt);
---------------------------------------------------------------------
return time;
}
警告符合:
time = g_strdup_printf (fmt, hours, minutes, seconds);
誰能幫助嗎?
UPDATE:
編譯
scan-build make CFLAGS='-Wformat-nonliteral'
你能告訴我們'g_strdup_printf'的原型嗎?這聽起來像你的參數不適合原型類型。 – dhein
通過以相反的順序執行操作,您可以避免發出警告:首先將小時,分鐘和秒值分別格式化爲其自己的字符串,然後將這些字符串與_(「%s%s%s」)一起格式化。 – ptomato