**
strncpy 跟 memcpy 的区别:
** strncpy 具有 置 0特性
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main()
{
char *a="abcdefg";
char b[10] = {'1','1','0','1','1','1','1','1','1','1'};
char c[10] = {'1','1','0','1','1','1','1','1','1','1'};
char d[10] = {'1','1','0','1','1','1','1','1','1','1'};
char e[11] = {'1','1','0','1','1','1','1','1','1','1','1'};
strncpy(b,a,6);
strncpy(c,a,7);
strncpy(e,a,10);
printf("b is %s\n",b);
printf("c is %s\n",c);
printf("e is %s\n",e);
printf("e[9] = %d\n",e[9]);
printf("e[10] = %d\n",e[10]);
}
输出结果
b is abcdef1111
c is abcdefg111@
e is abcdefg
e[9] = 0
e[10] = 49
可以看出 a[8] a[9] 被置为 0
参考: 【C代码】strncpy与memcpy区别
|