-
#include<iostream>
-
#include<assert.h>
-
using namespace std;
-
-
-
char* strncpy(char*desStr,const char *srcStr,int count)
-
{
-
assert(desStr!=NULL && srcStr!=NULL);
-
char *address=desStr;
-
while(count-- && (*srcStr)!='\0')
-
*desStr++=*srcStr++;
-
*desStr='\0'; //重点在于最后要加'\0'
-
return address;
-
-
}
-
-
int main()
-
{
-
int n;
-
char dest[100];
-
char* src="hello,world";
-
printf("please input the number you want to copy:");
-
scanf("%d",&n);
-
strncpy(dest,src,n);
-
printf("%s",&dest);
-
cout<<endl;
-
return 0;
-
-
- }