#include <stdio.h>
int main()
{
char name[20] = {0}; // 数组定义的时候需要初始化
const char* s = "asadds";
strncpy(name, s, strlen(s)); // 从s指针指向的内容中将长度为strlen(s)的字符串赋值到数组name中
printf("s = %s\n name = %s\n s_len = %d\n name_len = %d\n",s, name, strlen(s), sizeof(name));
return 0;
}
输出:
s = asadds
name = asadds
s_len = 6
name_len = 20