user-0 提问: 新手提问，为什么strcpy copy不了指定大小的字节？

回复:
user: user-1
content: 理解和讨论之前请先学会如何观察！  计算机组成原理→DOS命令→汇编语言→C语言（不包括C++）、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言（包括C++）、架构……  对学习编程者的忠告： 多用小脑和手，少用大脑、眼睛和嘴，会更快地学会编程！ 眼过千遍不如手过一遍！ 书看千行不如手敲一行！ 手敲千行不如单步一行！ 单步源代码千行不如单步Debug版对应汇编一行！ 单步Debug版对应汇编千行不如单步Release版对应汇编一行！ 不会单步Release版对应汇编？在你想单步Release版C/C++代码片断的前面临时加一句DebugBreak();重建所有，然后在IDE中运行。（一般人我不告诉他！）

user: user-2
content: 貌似如果string/strcpy.c就是实现的话，那么确实是要多拷贝一个byte了

user: user-0
content: 引用 7 楼 feekee 的回复:[quote=引用 4 楼 zhao4zhong1 的回复:] C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strcat.c /*** *strcat.c - contains strcat() and strcpy() * *       Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: *       Strcpy() copies one string onto another. * *       Strcat() concatenates (appends) a copy of the source string to the *       end of the destination string, returning the destination string. * *******************************************************************************/  #include <cruntime.h> #include <string.h>  #ifndef _MBSCAT #pragma function(strcat,strcpy) #endif  /* _MBSCAT */  /*** *char *strcat(dst, src) - concatenate (append) one string to another * *Purpose: *       Concatenates src onto the end of dest.  Assumes enough *       space in dest. * *Entry: *       char *dst - string to which "src" is to be appended *       const char *src - string to be appended to the end of "dst" * *Exit: *       The address of "dst" * *Exceptions: * *******************************************************************************/  char * __cdecl strcat (         char * dst,         const char * src         ) {         char * cp = dst;          while( *cp )                 cp++;                   /* find end of dst */          while( *cp++ = *src++ ) ;       /* Copy src to end of dst */          return( dst );                  /* return dst */  }   /*** *char *strcpy(dst, src) - copy one string over another * *Purpose: *       Copies the string src into the spot specified by *       dest; assumes enough room. * *Entry: *       char * dst - string over which "src" is to be copied *       const char * src - string to be copied over "dst" * *Exit: *       The address of "dst" * *Exceptions: *******************************************************************************/  char * __cdecl strcpy(char * dst, const char * src) {         char * cp = dst;          while( *cp++ = *src++ )                 ;               /* Copy src over dst */          return( dst ); }  感谢，受教了[/quote] 对了，我翻了一下glibc中strcpy函数，是这样的 [admin@localhost glibc-2.22]$ find ./ -name strcpy* ./debug/strcpy_chk.c ./sysdeps/ia64/strcpy.S ./sysdeps/sparc/sparc64/strcpy.S ./sysdeps/sparc/sparc32/strcpy.S ./sysdeps/sparc/sparc32/sparcv9/strcpy.S ./sysdeps/x86_64/strcpy.S ./sysdeps/x86_64/strcpy_chk.S ./sysdeps/x86_64/multiarch/strcpy.S ./sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S ./sysdeps/x86_64/multiarch/strcpy-ssse3.S ./sysdeps/s390/s390-64/strcpy.S ./sysdeps/s390/s390-32/strcpy.S ./sysdeps/aarch64/strcpy.S ./sysdeps/powerpc/powerpc32/405/strcpy.S ./sysdeps/powerpc/powerpc32/strcpy.S ./sysdeps/powerpc/powerpc64/power7/strcpy.S ./sysdeps/powerpc/powerpc64/strcpy.S ./sysdeps/powerpc/powerpc64/power8/strcpy.S ./sysdeps/powerpc/powerpc64/multiarch/strcpy-ppc64.S ./sysdeps/powerpc/powerpc64/multiarch/strcpy.c ./sysdeps/powerpc/powerpc64/multiarch/strcpy-power8.S ./sysdeps/powerpc/powerpc64/multiarch/strcpy-power7.S ./sysdeps/i386/i686/multiarch/strcpy-sse2.S ./sysdeps/i386/i686/multiarch/strcpy.S ./sysdeps/i386/i686/multiarch/strcpy-ssse3.S ./sysdeps/i386/i586/strcpy.S ./sysdeps/arm/armv6/strcpy.S ./sysdeps/alpha/strcpy.S ./string/strcpy.c [admin@localhost glibc-2.22]$ 汇编的那些看了头大，看了一下c语言文件./string/strcpy.c，是这样的 #include <stddef.h> #include <string.h>  #undef strcpy  /* Copy SRC to DEST.  */ char * strcpy (char *dest, const char *src) {   return memcpy (dest, src, strlen (src) + 1); } libc_hidden_builtin_def (strcpy)

user: user-0
content: 感谢大家的回复，之前一直没有感觉到strcpy还有这个特性，一般好像都是用strncpy多谢，感谢大家指出我的问题

user: user-0
content: 引用 4 楼 zhao4zhong1 的回复:C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strcat.c /*** *strcat.c - contains strcat() and strcpy() * *       Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: *       Strcpy() copies one string onto another. * *       Strcat() concatenates (appends) a copy of the source string to the *       end of the destination string, returning the destination string. * *******************************************************************************/  #include <cruntime.h> #include <string.h>  #ifndef _MBSCAT #pragma function(strcat,strcpy) #endif  /* _MBSCAT */  /*** *char *strcat(dst, src) - concatenate (append) one string to another * *Purpose: *       Concatenates src onto the end of dest.  Assumes enough *       space in dest. * *Entry: *       char *dst - string to which "src" is to be appended *       const char *src - string to be appended to the end of "dst" * *Exit: *       The address of "dst" * *Exceptions: * *******************************************************************************/  char * __cdecl strcat (         char * dst,         const char * src         ) {         char * cp = dst;          while( *cp )                 cp++;                   /* find end of dst */          while( *cp++ = *src++ ) ;       /* Copy src to end of dst */          return( dst );                  /* return dst */  }   /*** *char *strcpy(dst, src) - copy one string over another * *Purpose: *       Copies the string src into the spot specified by *       dest; assumes enough room. * *Entry: *       char * dst - string over which "src" is to be copied *       const char * src - string to be copied over "dst" * *Exit: *       The address of "dst" * *Exceptions: *******************************************************************************/  char * __cdecl strcpy(char * dst, const char * src) {         char * cp = dst;          while( *cp++ = *src++ )                 ;               /* Copy src over dst */          return( dst ); }  感谢，受教了

user: user-3
content: C 语言字符串末尾有一个结束符 '\0'，因此你的ABCDEF + '\0' 总共是7个字符，你定义的buf是6个字节，减掉一个字节用于存储 '\0' 后，只剩下5个字节可以使用了，因此，你strcpy(buf,arg[1]) arg[1] 传递5过去是没有问题的，传递6过去，就超出数组范围了。因此也就提示你Segmentation fault错误

user: user-4
content: 因为argv是字符串，每个参数带有一个值为0的结束符，所以当输入6个字母时，长度为7。

user: user-5
content: C 语言字符串末尾有一个结束符 '\0'

user: user-1
content: C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strcat.c /*** *strcat.c - contains strcat() and strcpy() * *       Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: *       Strcpy() copies one string onto another. * *       Strcat() concatenates (appends) a copy of the source string to the *       end of the destination string, returning the destination string. * *******************************************************************************/  #include <cruntime.h> #include <string.h>  #ifndef _MBSCAT #pragma function(strcat,strcpy) #endif  /* _MBSCAT */  /*** *char *strcat(dst, src) - concatenate (append) one string to another * *Purpose: *       Concatenates src onto the end of dest.  Assumes enough *       space in dest. * *Entry: *       char *dst - string to which "src" is to be appended *       const char *src - string to be appended to the end of "dst" * *Exit: *       The address of "dst" * *Exceptions: * *******************************************************************************/  char * __cdecl strcat (         char * dst,         const char * src         ) {         char * cp = dst;          while( *cp )                 cp++;                   /* find end of dst */          while( *cp++ = *src++ ) ;       /* Copy src to end of dst */          return( dst );                  /* return dst */  }   /*** *char *strcpy(dst, src) - copy one string over another * *Purpose: *       Copies the string src into the spot specified by *       dest; assumes enough room. * *Entry: *       char * dst - string over which "src" is to be copied *       const char * src - string to be copied over "dst" * *Exit: *       The address of "dst" * *Exceptions: *******************************************************************************/  char * __cdecl strcpy(char * dst, const char * src) {         char * cp = dst;          while( *cp++ = *src++ )                 ;               /* Copy src over dst */          return( dst ); }

user: user-6
content: 字符串还有结尾的\0，加上它之后你的字符数组就越界了

user: user-7
content: 字符串"ABCDEF"是7个字符，导致buf越界。因为strcpy会将'\0'拷贝至buf中，算上'\0'，就应该是7个字符，所以越界，越界访问导致段错误。 除去'\0'，buf最多容纳5个有效字符；