- //strcpy
- char *strcpy(char *strDest, const char *strSrc);
- {
- assert((strDest!=NULL) && (strSrc !=NULL));
- char *address = strDest;
- while( (*strDest++ = * strSrc++) != ‘/0’ )
- NULL ;
- return address ;
- }
- /*
- 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.
- 把src所指由NULL结束的字符串的前n个字节复制到dest所指的数组中。
- 如果src的前n个字节不含NULL字符,则结果不会以NULL字符结束;
- 如果src的长度小于n个字节,则以NULL填充dest直到复制完n个字节。
- src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
- 返回指向dest的指针。
- */
- char * my_strncpy( char * dest, const char * source, int count )
- {
- char *p = dest;
- while (count && (*p++ = *source++)) count--;
- while(count--)
- *p++ = '/0';
- return(dest);
- }
本文详细介绍了C语言中字符串复制函数strcpy和自定义函数my_strncpy的实现原理。讲解了如何安全地将源字符串复制到目标缓冲区,并在长度受限的情况下进行正确处理。强调了源和目标字符串不可重叠及目标缓冲区需足够大的限制条件。

被折叠的 条评论
为什么被折叠?



