诺亚方舟破土巴郎的ChinaUnix博客
诺亚方舟破土巴郎
全部博文(104)
2016年(104)
zds05
hm_zjh
薛定肚子
zhangsir
苍坠绯
淡泊心境
愿伊人静
svs121
王楠w_n
分类: C/C++
2016-10-06 11:04:05
原文地址:strncpy与memcpy 作者:lanlovehua
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char **argv) { char a[30] = "1234567890"; char b[30] = "abcdef\0ghijklmn"; int i; printf("a[]="); for(i = 0; i < 30; i++) printf("%c", a[i]); printf("\n");
strncpy(a, b, 20); printf("strncpy():a[]="); for(i = 0; i < 30; i++) printf("%c", a[i]); printf("\n");
memcpy(a, b, 20);
printf("memcpy():a[]="); for(i = 0; i < 30; i++) printf("%c", a[i]); printf("\n"); return 0; }
printf("memcpy():a[]=");
for(i = 0; i < 30; i++) printf("%c", a[i]); printf("\n"); return 0; }
在运行完之后我们会得到以下结果:
a[]=1234567890
strncpy():a[]=abcdef
memcpy():a[]=abcdefhijklmn
很明显我们就得出了strncpy与memcpy的区别:
当遇到\0时,strncpy停止复制,而memcpy会继续复制.
上一篇:原码,反码和补码
下一篇:uboot笔记之makefile分析
登录 注册