输入和输出字符串可以到的sprintf()是一样的吗? [英] Can the input and output strings to sprintf() be the same?

查看:226
本文介绍了输入和输出字符串可以到的sprintf()是一样的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用这种类型的公约多次在我的code在过去的:

I have used this type of convention many times in my code in the past:

strcpy ( cTmpA, "hello" );
sprintf ( cTmpA, "%s world", cTmpA );

最近,我打开我的遗产C编译器到Visual Studio 2005,发现我从上面的code产生的乱码字符串。然后,它发生,我认为可能的sprintf()的行为没有严格的定义,其中一个输入输出字符串相匹配。

Recently I switched my legacy C compiler to Visual Studio 2005, and found I got a garbled string resulting from the above code. It then occurred to me that perhaps the behaviour of sprintf() is not rigidly defined where one of the inputs matches the output string.

是上面code有效的K&安培; R C?如果没有,我怎么找都在我的code发生了此类型的呼叫到的sprintf()的地方呢?

Is the above code valid K&R C? If not, how do I find all the places in my code where this type of call to sprintf() has occurred?

推荐答案

虽然它是有效的K&放大器; RC,你可能想知道,而如果它是有效的POSIX - 看的 sprintf的规格。我们读到:

While it is valid K&R C, you probably want to rather know if it's valid POSIX - see sprintf Specification. We read:

如果复制操作为的sprintf()或snprintf的(一个调用的结果重叠的对象之间),结果是不确定的。

If copying takes place between objects that overlap as a result of a call to sprintf() or snprintf(), the results are undefined.

这篇关于输入和输出字符串可以到的sprintf()是一样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆