//模拟实现strncpy
#define _CRT_SECURE_NO_WARNINGS
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* my_strncpy(char*dest, const char* str, int count)
{
assert(dest != NULL&&str != NULL);
char *ret = dest;
while (count-- && (*dest++ = *str++))
{
;
}
if (count> 0)//当上述判断条件不为真时并且count未到零,在dest后继续加/0;
{
while (count--)
{
*dest++ = '/0';
}
}
return ret;
}
int main()
{
char arr1[8] = "abcde";
char arr2[10] = {0};
printf("%s", my_strncpy(arr2, arr1, 8));
system("pause");
return 0;
}