本文介绍了将字符串从const char *复制到char [500];的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
您能帮忙吗?
我的部分代码需要这样做
Hi all
can you help?
part of my code i need to do this
const char * p= "ddddd";<br />char q[500];<br /><br />I want to copy the sting in p to q[]?<br /><br />
推荐答案
strncpy( q, p, 499 );
q[499] = ''\0'';
可以解决问题,并且无论字符串多长都可以工作.如果您想进一步混淆事物,则可以利用strncpy的返回值将这两行合并为一:
will do the trick and work however long the string is. If you''d like to obfuscate things a bit more then you can combine the two lines into one by exploiting the return value of strncpy:
strncpy( q, p, 499 )[ 499 ] = ''\0'';
除非您希望在代码审查中部署极端讽刺,否则我不会使用单行版本.还有一些交叉眼的维护程序员.
干杯,
Ash
I wouldn''t use the single line version unless you want extreme sarcasm deployed in code reviews. And a few cross eyed maintenance programmers.
Cheers,
Ash
if(strlen(p)<500)
{
strcpy(q, p);
}
else
{
strncpy(q, p, 500-1);
}
如有任何问题,请随时与我联系:酷:!
Pls feel free to contact me if any question:cool:!
<br /> const char * p= "ddddd";<br /> char q[500];<br /> int i;<br /> i=0;<br /> while (p[i] && i < sizeof(q) - 1 )<br /> q[i++] = p[i];<br /> q[i]=''\0'';<br />
:)
:)
这篇关于将字符串从const char *复制到char [500];的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!