user-0 提问: 字符串c

回复:
user: user-1
content: 引用 8 楼 evanbao 的回复:[quote=引用 4 楼 zhao4zhong1 的回复:] /*** *strncpy.c - copy at most n characters of string * *       Copyright (c) 1985-2001, 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); }  弱弱的问一下，这个代码可以处理 src和dest内存地址比较靠近情况么？即src+sizeof(count*char)<dst[/quote] 请先弄清楚 copy和move的语义的不同之处： copy的意思是操作完成后，新目标和旧源相同，且旧源内容保持不变； move的意思是操作完成后，新目标和旧源相同，且旧源内容不一定保持不变； 所以根本不存在所谓源和目标内存重叠时可以实现的copy!

user: user-2
content: for + \0

user: user-3
content: 引用 4 楼 zhao4zhong1 的回复:/*** *strncpy.c - copy at most n characters of string * *       Copyright (c) 1985-2001, 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); }  弱弱的问一下，这个代码可以处理 src和dest内存地址比较靠近情况么？即src+sizeof(count*char)<dst

user: user-4
content: 恩库里面由函数的。strncpy

user: user-5
content: memmove呀

user: user-6
content: 引用 4 楼 zhao4zhong1 的回复:[/code]赵老你就是积极

user: user-7
content: 引用 2 楼 zxh707wk 的回复:自己写一个，这又不难 揍是，揍是，说的好像一定要帮他写出来似的

user: user-1
content: /*** *strncpy.c - copy at most n characters of string * *       Copyright (c) 1985-2001, 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-6
content: 自己写一个，这又不难

user: user-0
content: 不要直接用strncpy这个函数