user-0 提问: strncpy()？

回复:
user: user-1
content: 为什么不用memcpy呢？ memcpy Copies characters between buffers.  void *memcpy( void *dest, const void *src, size_t count );  Routine Required Header Compatibility  memcpy <memory.h> or <string.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  memcpy returns the value of dest.  Parameters  dest  New buffer  src  Buffer to copy from  count  Number of characters to copy  Remarks  The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.  Example  /* MEMCPY.C: Illustrate overlapping copy: memmove  * handles it correctly; memcpy does not.  */  #include <memory.h> #include <string.h> #include <stdio.h>  char string1[60] = "The quick brown dog jumps over the lazy fox"; char string2[60] = "The quick brown fox jumps over the lazy dog"; /*                           1         2         3         4         5  *                  12345678901234567890123456789012345678901234567890  */  void main( void ) {    printf( "Function:\tmemcpy without overlap\n" );    printf( "Source:\t\t%s\n", string1 + 40 );    printf( "Destination:\t%s\n", string1 + 16 );    memcpy( string1 + 16, string1 + 40, 3 );    printf( "Result:\t\t%s\n", string1 );    printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );     /* Restore string1 to original contents */    memcpy( string1 + 16, string2 + 40, 3 );     printf( "Function:\tmemmove with overlap\n" );    printf( "Source:\t\t%s\n", string2 + 4 );    printf( "Destination:\t%s\n", string2 + 10 );    memmove( string2 + 10, string2 + 4, 40 );    printf( "Result:\t\t%s\n", string2 );    printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );     printf( "Function:\tmemcpy with overlap\n" );    printf( "Source:\t\t%s\n", string1 + 4 );    printf( "Destination:\t%s\n", string1 + 10 );    memcpy( string1 + 10, string1 + 4, 40 );    printf( "Result:\t\t%s\n", string1 );    printf( "Length:\t\t%d characters\n\n", strlen( string1 ) ); }   Output  Function:   memcpy without overlap Source:      fox Destination:   dog jumps over the lazy fox Result:      The quick brown fox jumps over the lazy fox Length:      43 characters  Function:   memmove with overlap Source:      quick brown fox jumps over the lazy dog Destination:   brown fox jumps over the lazy dog Result:      The quick quick brown fox jumps over the lazy dog Length:      49 characters  Function:   memcpy with overlap Source:      quick brown dog jumps over the lazy fox Destination:   brown dog jumps over the lazy fox Result:      The quick quick brown dog jumps over the lazy fox Length:      49 characters   Buffer Manipulation Routines  See Also   _memccpy, memchr, memcmp, memmove, memset, strcpy, strncpy

user: user-2
content: char [ ] 字符数组类型，sizeof返回整个数组的占用的字节数 char * 字符指针类型，sizeof返回指针类型的字节数，32位系统是4字节  strlen的话，遇到  \0  就终止  想要长度的话，视具体情况而定

user: user-3
content: 请查看strncpy的函数原型 strncpy(char*,char*,n),不是你说的数组，可能你传进去实参是数组名，但是会被转化为char*，sizeof（）-1，至于为什么-1，是因为字符串后面有个\0 你使用sizeof(array)/sizeof(*array),这个很好解释，因为char是1个字节，int是4个字符，这样计算可以统一，不能是什么类型，sizeof(*array)代表的是一个元素的大小，而sizeof(array)整个实参所有数据的大小

user: user-4
content: //不管什么数据类型都是这样 sizeof(a) / sizeof(a[0])

user: user-5
content: 不对吧，sizeof使计算字节数的，要元素可以用strlen()，如果字符数组，存字符串，字符串元素小于串长你就不对了，char str[100]="China"; int a=sizeof(str);//a为100 int b = strlen(str);//b为5

user: user-6
content: sizeof(str1)-1 计算的当然不是数组元素个数   sizeof(str1)/sizeof(*str1)是通用的方法 能计算任何数组的元素个数 sizeof(str1) 只能计算出char[] 的元素个数 对于其他类型的数组计算的是 所占内存块的大小不是元素个数

user: user-7
content: 1 肯定是对的  字符数组中每个元素都是一个字节（char）  *str1 代表数组的首元素也是个char sizeof(str1)代表数组所占字节数