#include<iostream>
#include<stdio.h>
#include<assert.h>
char * my_strncpy( char *des,const char src,size_t len);
char * my_strncpy( char *des,const char *src,size_t len)
{
assert(des != NULL && src != NULL);
char * addr =NULL;
addr = des;
while(len-- && src != '\0')
{
*des = *src;
src++;
des++;
}
*des = '\0';
return addr;
}
int main()
{
char str[] = "liusenlin";
char des[20];
char * result = NULL;
result = my_strncpy(des,str,4);
printf("%s\n",result);
return 0;
}
运行结果:
lius
Press any key to continue