user-0 提问: 实现库函数strncpy

回复:
user: user-1

user: user-2
content: C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strncpy.c /*** *strncpy.c - copy at most n characters of string * *       Copyright (c) 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: 楼主看了你的代码，你在用*tmp++  当执行n次后tmp早已不是s的初始地址，反回tmp，在去解引地址会出现不可预料的事

user: user-4
content: #include<stdio.h> #include<stdlib.h> #include<string.h>  char * my_strncpy(char *buffer, const char *targe, int n) { 	if(buffer == NULL) 	{ 		fprintf(stderr,"buffer is a nunn pointer!\n"); 		return buffer; 	} 	int i; 	for( i = 0; i < n && targe != NULL; ++i) 	{ 		buffer[i] = targe[i]; 	} 	buffer[i] = '\0'; 	return buffer; }  int main(int argv, char ** argc) { 	char buffer[100]; 	const char *p = "Hello World"; 	printf("%s\n",my_strncpy(buffer,p,20));  	exit(EXIT_FAILURE);  }

user: user-4
content: char *s = "hello world!"; char *t = "boys"; 你这里定义的是字符串串常量,处于常量去,是不可以写的,即使i可以写,你也没有给buffer啊, 在一个问题定义h指针h常量应该是 const char *s = "hello world!";  const  char *t = "boys";

user: user-4
content: 看看库函数源码 /* Copyright (C) 1993-2014 Free Software Foundation, Inc.    This file is part of the GNU C Library.     The GNU C Library is free software; you can redistribute it and/or    modify it under the terms of the GNU Lesser General Public    License as published by the Free Software Foundation; either    version 2.1 of the License, or (at your option) any later version.     The GNU C Library is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    Lesser General Public License for more details.     You should have received a copy of the GNU Lesser General Public    License along with the GNU C Library; if not, see    <http://www.gnu.org/licenses/>.  */  /* This is almost copied from strncpy.c, written by Torbjorn Granlund.  */  #ifdef HAVE_CONFIG_H # include <config.h> #endif  #ifdef _LIBC # include <string.h> #else # include <sys/types.h> #endif  #ifndef STPNCPY # ifdef weak_alias #  define STPNCPY	__stpncpy weak_alias (__stpncpy, stpncpy) # else #  define STPNCPY	stpncpy # endif #endif  /* Copy no more than N characters of SRC to DEST, returning the address of    the terminating '\0' in DEST, if any, or else DEST + N.  */ char * STPNCPY (char *dest, const char *src, size_t n) {   char c;   char *s = dest;    if (n >= 4)     {       size_t n4 = n >> 2;        for (;;) 	{ 	  c = *src++; 	  *dest++ = c; 	  if (c == '\0') 	    break; 	  c = *src++; 	  *dest++ = c; 	  if (c == '\0') 	    break; 	  c = *src++; 	  *dest++ = c; 	  if (c == '\0') 	    break; 	  c = *src++; 	  *dest++ = c; 	  if (c == '\0') 	    break; 	  if (--n4 == 0) 	    goto last_chars; 	}       n -= dest - s;       goto zero_fill;     }   last_chars:   n &= 3;   if (n == 0)     return dest;    for (;;)     {       c = *src++;       --n;       *dest++ = c;       if (c == '\0') 	break;       if (n == 0) 	return dest;     }   zero_fill:   while (n-- > 0)     dest[n] = '\0';    return dest - 1; } #ifdef weak_alias libc_hidden_def (__stpncpy) #endif