user-0 提问: 自己写的一个strncpy（）函数。

回复:
user: user-1
content: 总是编译不通过，我按照提示错误，看不出来有问题啊 /*************************************************************************     > File Name: mystrcpy.c     > Author: Jukay     > Mail: hellojukay@gmail.com      > Created Time: 2014年10月23日 星期四 10时53分07秒  ************************************************************************/ #include<stdlib.h> #include<stdio.h> #include<string.h>  void mystrncpy(char *str1, char * str2, int n) { 	int i; 	if((n < strlen(str1) && strlen(str1) < strlen(str2)) ||( n < strlen(str2) && strlen(str2) < strlen(str1)) 	{ 		for(i=0; i < n; ++i) 			str1[i] = str2[i]; 		str1[i] ='\0'; 	} 	else if((strlen(str2) < n &&  n < strlen(str1)) || (strlen(str2) < strlen(str1) && strlen(str1) < n) 	{ 		for(i=0; i< strlen(str2); ++i) 			str1[i] = str2[i]; 		str1[i] = '\0'; 	} 	else if((strlen(str1) < strlen(str2) && strlen(str2)  < n ) || (strlen(str1) <  n && n < strlen(str2)) 	{ 		for(i=0; i< strlen(str1)-1; ++i) 			str1[i] = str2[i]; 		str1[i] = '\0'; 	} } int main() { 	char *str1 = (char *) malloc(10); 	char str2[] = "hello world"; 	mystrncpy(str1,str2, 5); 	printf("%s\n",str1); 	return 0; }

user: user-2
content: C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC\STRNCPY.C /*** *strncpy.c - copy at most n characters of string * *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved. * *Purpose: *       defines strncpy() - copy at most n characters of string * *******************************************************************************/  #include <cruntime.h> #include <string.h>  /*** *char *strncpy(dest, source, count) - copy at most n characters * *Purpose: *       Copies count characters from the source string to the *       destination.  If count is less than the length of source, *       NO NULL CHARACTER is put onto the end of the copied string. *       If count is greater than the length of sources, dest is padded *       with null characters to length count. * * *Entry: *       char *dest - pointer to destination *       char *source - source string for copy *       unsigned count - max number of characters to copy * *Exit: *       returns dest * *Exceptions: * *******************************************************************************/  char * __cdecl strncpy (         char * dest,         const char * source,         size_t count         ) {         char *start = dest;          while (count && (*dest++ = *source++))    /* copy string */                 count--;          if (count)                              /* pad out with zeroes */                 while (--count)                         *dest++ = '\0';          return(start); }

user: user-3
content: //字符串拷贝 char *StrCpy(char *pDes, const char *pSrc) { 	char *pTemp = pDes; 	while('\0' != *pSrc) 	{ 		*pTemp++ = *pSrc++; 	} 	*pTemp = '\0';  	return pDes; }  //指定长度字符串拷贝 char *StrnCpy(char *pDes, const char *pSrc, int nNums) { 	char *pTemp = pDes; 	int nTemp = nNums; 	while('\0' != *pSrc && nNums > 0) 	{ 		*pTemp++ = *pSrc++; 		nNums--; 	} 	*pTemp = '\0';  	return pDes; }

user: user-4
content: ch1[j] = '\0'; 数组越界了

user: user-5
content: #include<stdio.h> #include<string.h> void strncpy(char s1[],char s2[],int n) {   int lens2=strlen(s2);   int i=0,j;   if(lens2>=n)     {       while(i<n) 	{ 	  s1[i]=s2[i]; 	  i++; 	}       s1[i]=0;     }   else     {       i=0;       while(i<lens2) 	{ 	  s1[i]=s2[i]; 	  i++; 	}       s1[i]=0;      }    } int main() {   char str1[20],str2[20];   int n;   scanf("%s  %d",str2,&n);   strncpy(str1,str2,n);   printf("%s\n",str1);   return 0; }

user: user-6
content: ‘gets’ is deprecated in linux!  liam@ubuntu:~/practice$ ./a.out  dgdfgdfgdfgdfg 5 dgdfgdfgd Next one. sdfsdf3 3 sdf Next one. sdfgdfgdf4 4 sdfg Next one.  没看见有啥问题。不过真心难用。