连接问题 +伊托阿

发布于 2024-10-09 15:07:05 字数 643 浏览 7 评论 0原文

我有以下代码:

char stringHour[50], stringMinute[50], stringSecond[50];

// lots of code...
itoa(hour, stringHour, 10);
itoa(minute, stringMinute, 10);
itoa(second, stringSecond, 10);

strcat(":", stringSecond);
strcat(":", stringMinute);
strcat(stringMinute, stringSecond);
// stringHour contains both HH and :SS:MM
strcat(stringHour, stringMinute);
drawText(HRES/2 - 4, VRES - GLYPH_HEIGHT*2, 
stringHour, black);

小时分钟都是整数。

我想要做的是显示时间如下:HH:MM:SS。

变量保证在 0 到 59 之间(hout 除外,0-24)。

绘制文本是正确的。

这段代码使我的程序崩溃,但我找不到任何问题。你可以吗?

感谢您抽出时间!

I have the following code:

char stringHour[50], stringMinute[50], stringSecond[50];

// lots of code...
itoa(hour, stringHour, 10);
itoa(minute, stringMinute, 10);
itoa(second, stringSecond, 10);

strcat(":", stringSecond);
strcat(":", stringMinute);
strcat(stringMinute, stringSecond);
// stringHour contains both HH and :SS:MM
strcat(stringHour, stringMinute);
drawText(HRES/2 - 4, VRES - GLYPH_HEIGHT*2, 
stringHour, black);

hour, minute and second are all ints.

What I want to do is diplay time as follows: HH:MM:SS.

The variables are guaranteed to be between 0 and 59 (except hout, 0-24).

drawText is correct.

This block of code crashes my program, and I can't find anything wrong with it. Can you?

Thanks for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情深已缘浅 2024-10-16 15:07:05

这是错误的:

strcat(":", stringSecond);

您不能使用字符串作为第一个参数。请参阅 strcat 文档。

This is wrong:

strcat(":", stringSecond);

You can't use a string literal for the first argument. See strcat doc.

暖心男生 2024-10-16 15:07:05

失败的地方很可能是这一部分:

... stuff removed
strcat (":", 
... other stuff removed

您几乎肯定无法将 strcat 填充到您正在使用的平台上的字符串文字上。

strcat 方法也存在缓冲区溢出安全问题。也许使用 snprintf 可能是更好的选择?

More than likely the bit that is failing is this piece:

... stuff removed
strcat (":", 
... other stuff removed

You almost certainly can't strcat stuff onto a string literal on the platform you are using.

The strcat approach has buffer overflow safety issues as well. Maybe using snprintf might be a better alternative?

忘东忘西忘不掉你 2024-10-16 15:07:05

另外,最好将数组初始化为零。

char stringHour[50] = {0}, stringMinute[50] = {0}, stringSecond[50] = {0};

然后尝试将猫的结果存储到字符串中,看看你会得到什么。

Also, it is best to init arrays to zero.

char stringHour[50] = {0}, stringMinute[50] = {0}, stringSecond[50] = {0};

Then try storing the result of the cat into a string to see what you come up with.

陌路黄昏 2024-10-16 15:07:05

您不能修改字符串文字;使用 char buffer[] = "MyLiteral" 代替,然后使用 strcat()

(这一事实背后的原因是,编译器可以通过将字符串文字的多个实例合并到可执行文件的只读部分中的一个实例来优化代码大小,因此,如果您可以更改一个实例,那么您将更改其他所有实例,这会导致不可预测的行为。)

You can't modify string literals; use char buffer[] = "MyLiteral" instead and then strcat() to that.

(The reason behind this fact is that the compiler can optimize code size by consolidating multiple instances of your string literals into one instance in a read-only section of the executable, and so if you could change one instance, you'd change everything else, which would result in unpredictable behavior.)

不必在意 2024-10-16 15:07:05

另外 - 你的 strcat 是相反的顺序。 strcat 实际上是“连接”的 - 所以改变顺序。
strcat(stringHour, ":");
strcat(stringHour, stringMinute);
strcat(stringHour, ":");
strcat(stringHour, stringSecond);

然而,这非常糟糕——为什么不使用 sprintf 呢?另外 - 除了字符串构建之外的其他库 - 可能会查看 BSL。 C 标准库实际上也没有为您进行错误检查 - 所以这是搬起石头砸自己的脚,除非您确切地知道自己在做什么以及 str* 系列函数的期望是什么。

Also - your strcat is in reverse order. strcat actually "concatenates" - so change the order.
strcat(stringHour, ":");
strcat(stringHour, stringMinute);
strcat(stringHour, ":");
strcat(stringHour, stringSecond);

However, this is pretty terrible - why not use sprintf? Also - other libraries except for string building - possibly look into the BSL. The C standard library actually has no error checking for you either - so it's a shoot yourself in the foot, unless you know exactly what you're doing and what is expected from the str* family of functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文