char * strcpy(char *to, const char *from) { char *save = to; for (; (*to = *from) != ‘\0‘; ++from, ++to); return(save); }
char * strncpy ( char * destination, const char * source, size_t count ) { char *start = dest; while (count && (*dest++ = *source++)) count--; if (count) //如果count大于零 即count is greater than the length of sources,‘\0‘已写入,不够的字符数都填充\ while (--count) *dest++ = ‘\0‘; //继续写dest指向的字符 return(start); }
size_t strlcpy(char *dest, const char *src, size_t size) { size_t ret = strlen(src); if (size) { size_t len = (ret >= size) ? size - 1 : ret; //防止源字符串的越界问题 memcpy(dest, src, len); dest[len] = ‘\0‘; } return ret; }
char * strncpy ( char * destination, const char * source, size_t num )
时间: 2024-11-09 08:35:14