关于strncpy的问题。

lostgdi731 2004-08-05 03:52:52
char a[5],b[5],e[10],f[10];
int c,d,i;
ZeroMemory(a,5);
ZeroMemory(b,5);
ZeroMemory(e,10);
ZeroMemory(f,10);
c=1000;
d=1001;
strncpy(a,(char*)&c,sizeof(int));
strncpy(b,(char*)&d,sizeof(int));

strncpy(e,a,sizeof(int));
strncpy(e+sizeof(int),b,sizeof(int));

strncpy(f,e,10);
----------------
本来我的意思就是想f这个字符串的内容和e字符串完全相同的,但执行最后一句的时候居然不成功。
如果把最后一句话改成:
for(i=0;i<10;i++)
{
strcpy(&f[i],&e[i]);
}
的话,就可以。那请问这是什么问题?
...全文
240 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jim3000 2004-08-06
  • 打赏
  • 举报
回复
whale() 说得很对!按他的解决办法没错,最好用memcpy
你的程序出的问题是:
strncpy(a,(char*)&c,sizeof(int));//因为c = 1000,是int,它的内存里,第一个字节为0(相当于NULL),执行后的结果,a还是空值。
。。。
strncpy(e,a,sizeof(int));//这样,得到的e还是空值

strncpy(f,e,10);//最后,得到的f还是空值



kvw3000 2004-08-06
  • 打赏
  • 举报
回复
zhangyilan(数字通信)正解
carbon107 2004-08-06
  • 打赏
  • 举报
回复
zhangyilan(数字通信)说的好
holyeagle 2004-08-06
  • 打赏
  • 举报
回复
这是因为你用的是strncpy,而不是memcpy。
strncpy进行的是字符串复制,你的e中在复制完a和b以后,是一个e8 03 00 00 e9 03 00 00,strncpy复制的时候遇到第一个00的时候,会认为字符串结束,从而只复制e8 03
zhangyilan 2004-08-06
  • 打赏
  • 举报
回复
char a[5],b[5],e[10],f[10];
int c,d,i;
ZeroMemory(a,5);
ZeroMemory(b,5);
ZeroMemory(e,10);
ZeroMemory(f,10);
c=1000;
d=1001;
strncpy(a,(char*)&c,sizeof(int));//int是32位,相当于占了4个char,但是1000只占用16位,所有a[2],a[3]均为0。
strncpy(b,(char*)&d,sizeof(int));//同理,b[2],b[3]也为0.

strncpy(e,a,sizeof(int));
strncpy(e+sizeof(int),b,sizeof(int));//这两步操作后,e[2]还是为0;

strncpy(f,e,10);//应该是碰到了e[2]中的0,拷贝就停止了。

strncpy说明
如果src的前n个字节不含NULL字符,则结果不会以NULL字符结束。
如果src的长度小于n个字节,则以NULL填充dest直到复制完n个字节。
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
whale 2004-08-06
  • 打赏
  • 举报
回复
strncpy(a,(char*)&c,sizeof(int));
strncpy(b,(char*)&d,sizeof(int));
看不懂你在干什么。如果想把c,d转成字符串保存在a,b中,也不是这样做啊,应该使用itoa之类的。如果是想把c,d的数值保存到a,b里面,直接用*(int*)a = c就可以了,或者用memcpy
lostgdi731 2004-08-06
  • 打赏
  • 举报
回复
再UP一下。
lostgdi731 2004-08-05
  • 打赏
  • 举报
回复
1楼我不知道你在说什么。你自己不会别乱说啊。
2楼的,你答非所问啊,不知你测试过没,很奇怪的。
newred1973 2004-08-05
  • 打赏
  • 举报
回复
The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.



你的字串长度非常明确,为什么不用:
strncpy(f,e,sizeof(int)*2);

DentistryDoctor 2004-08-05
  • 打赏
  • 举报
回复
f,e都只有10个字节,但字符串末需要一个'\0'呀,如果你最后一行的第三个参数小于10就会成功。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧