连接问题 +伊托阿
我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是错误的:
您不能使用字符串作为第一个参数。请参阅 strcat 文档。
This is wrong:
You can't use a string literal for the first argument. See strcat doc.
失败的地方很可能是这一部分:
您几乎肯定无法将
strcat
填充到您正在使用的平台上的字符串文字上。strcat
方法也存在缓冲区溢出安全问题。也许使用 snprintf 可能是更好的选择?More than likely the bit that is failing is this piece:
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 usingsnprintf
might be a better alternative?另外,最好将数组初始化为零。
然后尝试将猫的结果存储到字符串中,看看你会得到什么。
Also, it is best to init arrays to zero.
Then try storing the result of the cat into a string to see what you come up with.
您不能修改字符串文字;使用
char buffer[] = "MyLiteral"
代替,然后使用strcat()
。(这一事实背后的原因是,编译器可以通过将字符串文字的多个实例合并到可执行文件的只读部分中的一个实例来优化代码大小,因此,如果您可以更改一个实例,那么您将更改其他所有实例,这会导致不可预测的行为。)
You can't modify string literals; use
char buffer[] = "MyLiteral"
instead and thenstrcat()
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.)
另外 - 你的 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.